useXTable.ts 1.5 KB

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