index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <page-layout-list :no-padding="layoutNoPadding" :compact="layoutCompact">
  3. <template #filter v-if="withSearch">
  4. <cip-search-form
  5. v-model:model="searchFilter"
  6. :defaultModel="cDefaultSearchFilter"
  7. :field-list="searchFieldList"
  8. :hide-search="hideSearchButton"
  9. :list-loading="listLoading"
  10. :handleAbsolute="searchHandleAbsolute"
  11. @search="search" />
  12. </template>
  13. <template #handle v-if="withHandle">
  14. <slot name="handle-buttons"
  15. :create-item="createItem"
  16. :delete-item-list="deleteItemList"
  17. :get-select-rows="getSelectRows"
  18. :get-item-list="getItemList">
  19. </slot>
  20. <template v-if="withCreate">
  21. <cip-judge-privilege :privilege="permission.create">
  22. <cip-button button-type="create" @click="createItem(outParams)"></cip-button>
  23. </cip-judge-privilege>
  24. </template>
  25. <template v-if="batchDelete">
  26. <cip-judge-privilege :privilege="permission.batchDelete ?? permission.delete">
  27. <cip-button button-type="batchDelete" @click="deleteItemList(getSelectRows)"></cip-button>
  28. </cip-judge-privilege>
  29. </template>
  30. </template>
  31. <slot></slot>
  32. <!-- 从handle中去除 原因:withHandle false 将导致table内的 关于弹窗的按钮无法使用-->
  33. <cip-dialog v-model="itemDialog" :size="dialogSize" :width="dialogWidth" :on-confirm="saveItem" :title="dialogTitle" :show-only="showOnly">
  34. <cip-form :label-width="formLabelWidth"
  35. v-model:model="item"
  36. :grid="formGrid"
  37. :label-suffix="formLabelSuffix"
  38. :show-only="showOnly"
  39. :field-list="saveFormFieldList"></cip-form>
  40. <template #footer="{confirm,loading,cancel}" v-if="$slots['dialog-handle']">
  41. <slot name="dialog-handle" v-bind="{confirm,loading,cancel}" :model="item" :get-item-list="getItemList"></slot>
  42. </template>
  43. </cip-dialog>
  44. <cip-table :data="itemList"
  45. :columns="tableColumns"
  46. ref="tableRef"
  47. :default-expand-all="defaultExpandAll"
  48. :row-key="tableRowKey"
  49. :tree-props="tableTreeProps"
  50. :offset="withIndex ? offset : undefined"
  51. :select-type="selectType"
  52. :selectable="tableSelectable"
  53. @update:selectColumns="selectColumns"
  54. v-bind="tableAttrs"
  55. @sort="sortChange"
  56. v-loading="listLoading"
  57. :handlerWidth="tableHandleWidth"
  58. :withTableHandle="withTableHandle">
  59. <slot name="table-slots"></slot>
  60. <template #expand="scope" v-if="$slots.expand">
  61. <slot name="expand" v-bind="scope"></slot>
  62. </template>
  63. <!-- <el-table-column label="操作" :width="tableHandleWidth" v-if="withTableHandle" fixed="right">-->
  64. <!-- <template #default="scope">-->
  65. <template #_handler="scope" v-if="withTableHandle">
  66. <slot name="table-handle-prepend" v-bind="scope" :get-item-list="getItemList"></slot>
  67. <slot name="table-handle"
  68. v-bind="scope"
  69. :show-item="showItem"
  70. :delete-item="deleteItem"
  71. :edit-item="editItem"
  72. :get-item-list="getItemList">
  73. <cip-judge-privilege :privilege="permission.info">
  74. <cip-button-text @click="showItem(scope.row, fetchInfo)">查看</cip-button-text>
  75. </cip-judge-privilege>
  76. <cip-judge-privilege :privilege="permission.update">
  77. <cip-button-text @click="editItem(scope.row, fetchInfo)" :disabled="judgeTableEditButtonDisabledFn && judgeTableEditButtonDisabledFn(scope)">修改</cip-button-text>
  78. </cip-judge-privilege>
  79. <template v-if="withTableDeleteButton &&(!judgeTableDeleteButtonFn || (judgeTableDeleteButtonFn && judgeTableDeleteButtonFn(scope)))" >
  80. <cip-judge-privilege :privilege="permission.delete">
  81. <cip-button-text type="danger" @click="deleteItem(scope.row)">删除</cip-button-text>
  82. </cip-judge-privilege>
  83. </template>
  84. </slot>
  85. <slot name="table-handle-append" v-bind="scope" :get-item-list="getItemList"></slot>
  86. </template>
  87. </cip-table>
  88. <!-- </template>-->
  89. <!-- </el-table-column>-->
  90. <template #pagination v-if="withPagination">
  91. <cip-pagination v-bind="paginationConfig"
  92. v-model:offset="offset"
  93. v-model:limit="limit"
  94. :total="total"
  95. :current-page="currentPage"
  96. :pageSizes="pageSizes"
  97. @refresh="getItemList"></cip-pagination>
  98. </template>
  99. </page-layout-list>
  100. </template>
  101. <script>
  102. import { computed, defineComponent, onActivated, ref, watchEffect } from 'vue'
  103. import { ElLoading } from 'element-plus'
  104. import PageLayoutList from '@cip/components/page-layout/list'
  105. import CipSearchForm from '@cip/components/cip-search-form'
  106. import CipForm from '@cip/components/cip-form'
  107. import CipButton from '@cip/components/cip-button'
  108. import CipDialog from '@cip/components/cip-dialog'
  109. import CipTable from '@cip/components/cip-table'
  110. import CipButtonText from '@cip/components/cip-button-text'
  111. import CipPagination from '@cip/components/cip-pagination'
  112. import CipJudgePrivilege from '@cip/components/cip-judge-privilege'
  113. import { useCurd } from '@cip/hooks/use-curd'
  114. import { isEmpty, getUsingConfig } from '@cip/utils/util'
  115. import CipTableHandler from '@cip/components/cip-table-handler'
  116. import pageCurdProps from './props'
  117. import { useCipConfig } from '@cip/components/hooks/use-cip-config'
  118. export default defineComponent({
  119. name: 'CipPageCurd',
  120. components: {
  121. PageLayoutList,
  122. CipSearchForm,
  123. CipPagination,
  124. CipTable,
  125. CipForm,
  126. CipButton,
  127. CipDialog,
  128. CipButtonText,
  129. CipTableHandler,
  130. CipJudgePrivilege
  131. },
  132. directives: {
  133. loading: ElLoading.directive
  134. },
  135. props: pageCurdProps,
  136. setup (props, { emit, slots }) {
  137. const cipConfig = useCipConfig()
  138. const tableRef = ref()
  139. const CURD = useCurd(props.entity, { ...props.curdFn, itemType: props.itemType })
  140. const { searchFilter, defaultSearchFilter: cDefaultSearchFilter, getItemList, limit } = CURD
  141. // eslint-disable-next-line vue/no-setup-props-destructure
  142. if (typeof props.tableDefaultLimit === 'number') limit.value = props.tableDefaultLimit // (一次性)
  143. watchEffect(() => {
  144. cDefaultSearchFilter.value = { ...props.outParams, ...props.defaultSearchFilter }
  145. })
  146. // const defaultSearchModel = computed(() => {
  147. // return { ...props.outParams, ...props.defaultSearchFilter }
  148. // })
  149. // 搜索部分处理按钮是否在特定条件下开启绝对定位
  150. const searchHandleAbsolute = computed(() => {
  151. const compact = getUsingConfig(props.layoutCompact, cipConfig.layout.compact)
  152. if (!compact) return false
  153. if (!props.withHandle) return false
  154. return !!(props.withCreate || props.batchDelete || slots['handle-buttons'])
  155. })
  156. // searchFilter.value = { ...defaultSearchModel.value, ...searchFilter.value }
  157. // if (props.cache) {
  158. onActivated(() => {
  159. getItemList()
  160. })
  161. if (!props.autoSelected) { // 如果搜索中有autoSelect的字段则传true,避免请求两次数据不一致导致页面“跳动”
  162. getItemList()
  163. }
  164. // } else {
  165. // if (!props.autoSelected) { // 如果搜索中有autoSelect的字段则传true,避免请求两次数据不一致导致页面“跳动”
  166. // getItemList()
  167. // }
  168. // }
  169. // 计算列表是否需要select
  170. const selectType = computed(() => {
  171. if (props.batchDelete) return 'checkbox'
  172. return props.tableSelectType
  173. // return undefined
  174. })
  175. const getSelectRows = () => { // 返回值 Ref<T[]>
  176. return tableRef.value.cipTableRef.store.states.selection
  177. }
  178. // 外层触发请求
  179. const getTableList = (item = {}) => {
  180. searchFilter.value = Object.assign(searchFilter.value, item) // { ...item }
  181. getItemList()
  182. }
  183. // 排序
  184. const sortChange = ({ prop, order }) => {
  185. searchFilter.value = { ...searchFilter.value, ...{ orderBy: prop, orderAsc: isEmpty(order) ? undefined : order === 'ascending' } }
  186. getItemList()
  187. }
  188. const saveFormFieldList = computed(() => {
  189. if (props.updateFormFieldList) { // 如果存在updateFormFieldList则对当前进行判断
  190. return CURD.isUpdate.value ? props.updateFormFieldList : props.formFieldList
  191. } else {
  192. return props.formFieldList
  193. }
  194. })
  195. const selectColumns = (val) => { // table组件选择列表抛出
  196. emit('selectColumns', val)
  197. }
  198. return {
  199. ...CURD,
  200. ...props.componentData, // 覆盖默认配置
  201. cDefaultSearchFilter,
  202. searchHandleAbsolute,
  203. tableRef,
  204. saveFormFieldList,
  205. selectType,
  206. getSelectRows,
  207. getTableList,
  208. sortChange,
  209. selectColumns
  210. }
  211. }
  212. })
  213. </script>