useTable.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { Table, TableExpose } from '@/components/Table'
  2. import { ElTable } from 'element-plus'
  3. import { ref, reactive, watch, computed, unref, nextTick } from 'vue'
  4. import { AxiosPromise } from 'axios'
  5. import { get, assign } from 'lodash-es'
  6. import type { TableProps } from '@/components/Table/src/types'
  7. interface UseTableConfig<T, L> {
  8. getListApi: (option: L) => AxiosPromise<T>
  9. // 返回数据格式配置
  10. response: {
  11. list: string
  12. total?: string
  13. }
  14. }
  15. interface TableObject<K, L> {
  16. pageSize: number
  17. currentPage: number
  18. total: number
  19. tableList: K[]
  20. parmasObj: L
  21. loading: boolean
  22. }
  23. export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
  24. config?: UseTableConfig<T, L>
  25. ) => {
  26. const tableObject = reactive<TableObject<K, L>>({
  27. // 页数
  28. pageSize: 10,
  29. // 当前页
  30. currentPage: 1,
  31. // 总条数
  32. total: 10,
  33. // 表格数据
  34. tableList: [],
  35. // AxiosConfig 配置
  36. parmasObj: {} as L,
  37. // 加载中
  38. loading: true
  39. })
  40. const parmasObj = computed(() => {
  41. return assign(
  42. {
  43. params: {
  44. pageSize: tableObject.pageSize,
  45. pageIndex: tableObject.currentPage
  46. }
  47. },
  48. tableObject.parmasObj
  49. )
  50. })
  51. watch(
  52. () => tableObject.currentPage,
  53. () => {
  54. methods.getList()
  55. }
  56. )
  57. watch(
  58. () => tableObject.pageSize,
  59. () => {
  60. tableObject.currentPage = 1
  61. methods.getList()
  62. }
  63. )
  64. // Table实例
  65. const tableRef = ref<typeof Table & TableExpose>()
  66. // ElTable实例
  67. const elTableRef = ref<ComponentRef<typeof ElTable>>()
  68. const register = (ref: typeof Table & TableExpose, elRef: ComponentRef<typeof ElTable>) => {
  69. tableRef.value = ref
  70. elTableRef.value = elRef
  71. }
  72. const getTable = async () => {
  73. await nextTick()
  74. const table = unref(tableRef)
  75. if (!table) {
  76. console.error('The table is not registered. Please use the register method to register')
  77. }
  78. return table
  79. }
  80. const methods = {
  81. getList: async () => {
  82. tableObject.loading = true
  83. const res = await config
  84. ?.getListApi(unref(parmasObj) as L)
  85. .catch(() => {})
  86. .finally(() => {
  87. tableObject.loading = false
  88. })
  89. if (res) {
  90. tableObject.tableList = get(res.data || {}, config?.response.list as string)
  91. tableObject.total = get(res.data || {}, config?.response?.total as string) || 0
  92. }
  93. },
  94. setProps: async (props: TableProps = {}) => {
  95. const table = await getTable()
  96. table?.setProps(props)
  97. }
  98. }
  99. return {
  100. register,
  101. tableObject,
  102. methods
  103. }
  104. }