index.vue 9.0 KB

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