useXTable.ts 998 B

1234567891011121314151617181920212223242526272829303132
  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: (ids: string | number) => void
  7. exportList: (fileName?: string) => void
  8. }
  9. export function useXTable(props: XTableProps): [Function, tableMethod] {
  10. const tableRef = ref<Nullable<tableMethod>>(null)
  11. function register(instance) {
  12. tableRef.value = instance
  13. props && instance.setProps(props)
  14. }
  15. function getInstance(): tableMethod {
  16. const table = unref(tableRef)
  17. if (!table) {
  18. console.error('表格实例不存在')
  19. }
  20. return table as tableMethod
  21. }
  22. const methods: tableMethod = {
  23. reload: () => getInstance().reload(),
  24. setProps: (props) => getInstance().setProps(props),
  25. deleteData: (ids: string | number) => getInstance().deleteData(ids),
  26. exportList: (fileName?: string) => getInstance().exportList(fileName)
  27. }
  28. return [register, methods]
  29. }