index.vue 8.8 KB

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