useTable.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { useI18n } from '@/hooks/web/useI18n'
  2. import { Table, TableExpose, TableProps, TableSetProps, TableColumn } from '@/components/Table'
  3. import { ElTable, ElMessageBox, ElMessage } from 'element-plus'
  4. import { ref, watch, unref, nextTick, onMounted } from 'vue'
  5. const { t } = useI18n()
  6. interface UseTableConfig {
  7. /**
  8. * 是否初始化的时候请求一次
  9. */
  10. immediate?: boolean
  11. fetchDataApi: () => Promise<{
  12. list: any[]
  13. total?: number
  14. }>
  15. fetchDelApi?: () => Promise<boolean>
  16. }
  17. export const useTable = (config: UseTableConfig) => {
  18. const { immediate = true } = config
  19. const loading = ref(false)
  20. const currentPage = ref(1)
  21. const pageSize = ref(10)
  22. const total = ref(0)
  23. const dataList = ref<any[]>([])
  24. watch(
  25. () => currentPage.value,
  26. () => {
  27. methods.getList()
  28. }
  29. )
  30. watch(
  31. () => pageSize.value,
  32. () => {
  33. // 当前页不为1时,修改页数后会导致多次调用getList方法
  34. if (unref(currentPage) === 1) {
  35. methods.getList()
  36. } else {
  37. currentPage.value = 1
  38. methods.getList()
  39. }
  40. }
  41. )
  42. onMounted(() => {
  43. if (immediate) {
  44. methods.getList()
  45. }
  46. })
  47. // Table实例
  48. const tableRef = ref<typeof Table & TableExpose>()
  49. // ElTable实例
  50. const elTableRef = ref<ComponentRef<typeof ElTable>>()
  51. const register = (ref: typeof Table & TableExpose, elRef: ComponentRef<typeof ElTable>) => {
  52. tableRef.value = ref
  53. elTableRef.value = unref(elRef)
  54. }
  55. const getTable = async () => {
  56. await nextTick()
  57. const table = unref(tableRef)
  58. if (!table) {
  59. console.error('The table is not registered. Please use the register method to register')
  60. }
  61. return table
  62. }
  63. const methods = {
  64. /**
  65. * 获取表单数据
  66. */
  67. getList: async () => {
  68. loading.value = true
  69. try {
  70. const res = await config?.fetchDataApi()
  71. console.log('fetchDataApi res', res)
  72. if (res) {
  73. dataList.value = res.list
  74. total.value = res.total || 0
  75. }
  76. } catch (err) {
  77. console.log('fetchDataApi error')
  78. } finally {
  79. loading.value = false
  80. }
  81. },
  82. /**
  83. * @description 设置table组件的props
  84. * @param props table组件的props
  85. */
  86. setProps: async (props: TableProps = {}) => {
  87. const table = await getTable()
  88. table?.setProps(props)
  89. },
  90. /**
  91. * @description 设置column
  92. * @param columnProps 需要设置的列
  93. */
  94. setColumn: async (columnProps: TableSetProps[]) => {
  95. const table = await getTable()
  96. table?.setColumn(columnProps)
  97. },
  98. /**
  99. * @description 新增column
  100. * @param tableColumn 需要新增数据
  101. * @param index 在哪里新增
  102. */
  103. addColumn: async (tableColumn: TableColumn, index?: number) => {
  104. const table = await getTable()
  105. table?.addColumn(tableColumn, index)
  106. },
  107. /**
  108. * @description 删除column
  109. * @param field 删除哪个数据
  110. */
  111. delColumn: async (field: string) => {
  112. const table = await getTable()
  113. table?.delColumn(field)
  114. },
  115. /**
  116. * @description 获取ElTable组件的实例
  117. * @returns ElTable instance
  118. */
  119. getElTableExpose: async () => {
  120. await getTable()
  121. return unref(elTableRef)
  122. },
  123. refresh: () => {
  124. methods.getList()
  125. },
  126. // sortableChange: (e: any) => {
  127. // console.log('sortableChange', e)
  128. // const { oldIndex, newIndex } = e
  129. // dataList.value.splice(newIndex, 0, dataList.value.splice(oldIndex, 1)[0])
  130. // // to do something
  131. // }
  132. // 删除数据
  133. delList: async (idsLength: number) => {
  134. const { fetchDelApi } = config
  135. if (!fetchDelApi) {
  136. console.warn('fetchDelApi is undefined')
  137. return
  138. }
  139. ElMessageBox.confirm(t('common.delMessage'), t('common.delWarning'), {
  140. confirmButtonText: t('common.delOk'),
  141. cancelButtonText: t('common.delCancel'),
  142. type: 'warning'
  143. }).then(async () => {
  144. const res = await fetchDelApi()
  145. if (res) {
  146. ElMessage.success(t('common.delSuccess'))
  147. // 计算出临界点
  148. const current =
  149. unref(total) % unref(pageSize) === idsLength || unref(pageSize) === 1
  150. ? unref(currentPage) > 1
  151. ? unref(currentPage) - 1
  152. : unref(currentPage)
  153. : unref(currentPage)
  154. currentPage.value = current
  155. methods.getList()
  156. }
  157. })
  158. }
  159. }
  160. return {
  161. tableRegister: register,
  162. tableMethods: methods,
  163. tableState: {
  164. currentPage,
  165. pageSize,
  166. total,
  167. dataList,
  168. loading
  169. }
  170. }
  171. }