useXTable.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { XTableProps } from '@/components/XTable/src/type'
  2. export interface tableMethod {
  3. reload: () => void // 刷新表格
  4. setProps: (props: XTableProps) => void
  5. deleteData: (id: string | number) => void // 删除数据
  6. deleteBatch: () => void // 批量删除
  7. exportList: (fileName?: string) => void // 导出列表
  8. getCurrentColumn: () => void // 获取当前列
  9. getRadioRecord: () => void // 获取当前选中列,radio
  10. getCheckboxRecords: () => void //获取当前选中列, checkbox
  11. }
  12. export const useXTable = (props: XTableProps): [Function, tableMethod] => {
  13. const tableRef = ref<Nullable<tableMethod>>(null)
  14. const register = (instance) => {
  15. tableRef.value = instance
  16. props && instance.setProps(props)
  17. }
  18. const getInstance = (): tableMethod => {
  19. const table = unref(tableRef)
  20. if (!table) {
  21. console.error('表格实例不存在')
  22. }
  23. return table as tableMethod
  24. }
  25. const methods: tableMethod = {
  26. reload: () => getInstance().reload(),
  27. setProps: (props) => getInstance().setProps(props),
  28. deleteData: (id: string | number) => getInstance().deleteData(id),
  29. deleteBatch: () => getInstance().deleteBatch(),
  30. exportList: (fileName?: string) => getInstance().exportList(fileName),
  31. getCurrentColumn: () => getInstance().getCheckboxRecords(),
  32. getRadioRecord: () => getInstance().getRadioRecord(),
  33. getCheckboxRecords: () => getInstance().getCheckboxRecords()
  34. }
  35. return [register, methods]
  36. }