useTable.ts 4.9 KB

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