useTablePage.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { reactive, ref, computed, /*getCurrentInstance,*/ unref, watch, nextTick, shallowRef } from 'vue'
  2. import { $log } from '@/utils'
  3. // import { ObjectOpts } from '@/components/FormConfig/formConfig.types.ts'
  4. import { LeTableColumnProps, LeTableProps, SearchParams } from '@/components/Table'
  5. type SearchData = { [prop: string]: any }
  6. export type UseTableConfig = {
  7. // 搜索表单数据
  8. searchData: SearchData
  9. // 更新searchParams参数数据
  10. updateParams: () => void
  11. // 请求表格数据方法
  12. queryList: () => void
  13. // 是否初始化请求
  14. fetchImmediate?: boolean
  15. }
  16. export const useTablePage = (tableProps: Partial<LeTableProps> = {}, config: Partial<UseTableConfig> = {}) => {
  17. const localConfig: UseTableConfig = Object.assign(
  18. {
  19. searchData: {},
  20. fetchImmediate: true,
  21. queryList: () => $log('请添加queryList', JSON.stringify(tableOpts.searchParams)),
  22. updateParams: () => {
  23. tableOpts.searchParams = {
  24. ...(tableOpts.searchParams as SearchParams),
  25. ...searchData.value,
  26. // ...todo 若有更多操作 searchData 进行请求,请覆盖updateParams 方法
  27. page: 1
  28. }
  29. }
  30. },
  31. config
  32. )
  33. const curSelectionRows = ref<any[]>([])
  34. const selectionChange = (rows: any[]) => {
  35. // console.error('rows...', rows)
  36. curSelectionRows.value = rows
  37. }
  38. const tableOpts = reactive<LeTableProps>({
  39. total: 0,
  40. list: [],
  41. columns: [],
  42. /*columnsConfig: {
  43. columns: [],
  44. defaultCheckedOptions: []
  45. }*/
  46. ...tableProps,
  47. searchParams: {
  48. page: 1,
  49. pageSize: 20,
  50. ...(tableProps.searchParams || {})
  51. },
  52. options: {
  53. loading: false,
  54. showOverflowTooltip: false,
  55. multipleSelect: true, // 是否支持列表项选中功能
  56. // 多选变更
  57. onSelectionChange: selectionChange,
  58. // stripe: true, // 是否为斑马纹 table
  59. // highlightCurrentRow: true, // 是否支持当前行高亮显示
  60. ...(tableProps.options || {})
  61. }
  62. })
  63. const searchData = ref<SearchData>({
  64. ...config.searchData
  65. })
  66. const checkedColumns = shallowRef(tableOpts.columns)
  67. const activeColumns = computed(() => {
  68. const _checkedColumns = unref(checkedColumns) as LeTableColumnProps[]
  69. if (!_checkedColumns.length) return tableOpts.columns
  70. return _checkedColumns
  71. .map(v => {
  72. const cur = tableOpts.columns.find(column => column.prop === v.prop)
  73. if (cur) {
  74. const fixedFlag = cur.fixed
  75. if (fixedFlag) {
  76. // eslint-disable-next-line
  77. // @ts-ignore
  78. v.fixed = fixedFlag
  79. }
  80. return cur
  81. }
  82. })
  83. .filter(Boolean)
  84. })
  85. /*const instance = getCurrentInstance()
  86. console.error(instance, 'instance.proxy')*/
  87. watch(
  88. () => searchData.value,
  89. () => {
  90. nextTick().then(localConfig.updateParams)
  91. },
  92. {
  93. immediate: localConfig.fetchImmediate
  94. }
  95. )
  96. // watch(() => tableOpts.searchParams.custName, (v, oldv) => {
  97. // watch(tableOpts.searchParams, (v, oldv) => {
  98. watch(
  99. () => tableOpts.searchParams,
  100. // (v, oldV) => {
  101. /*() => {
  102. nextTick(() => {
  103. localConfig.queryList?.()
  104. })
  105. },*/
  106. localConfig.queryList,
  107. {
  108. // immediate: localConfig.fetchImmediate
  109. }
  110. )
  111. return {
  112. tableOpts,
  113. checkedColumns,
  114. activeColumns,
  115. // 多选数据
  116. curSelectionRows,
  117. // 搜索表单数据
  118. searchData,
  119. // 刷新数据方法
  120. updateParams: localConfig.updateParams
  121. }
  122. }