index.vue 10.0 KB

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