useTable.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import type { BasicTableProps, TableActionType, FetchParams, BasicColumn } from '../types/table';
  2. import type { PaginationProps } from '../types/pagination';
  3. import type { DynamicProps } from '/#/utils';
  4. import type { FormActionType } from '/@/components/Form';
  5. import type { WatchStopHandle } from 'vue';
  6. import { getDynamicProps } from '/@/utils';
  7. import { ref, onUnmounted, unref, watch, toRaw } from 'vue';
  8. import { isProdMode } from '/@/utils/env';
  9. import { error } from '/@/utils/log';
  10. type Props = Partial<DynamicProps<BasicTableProps>>;
  11. type UseTableMethod = TableActionType & {
  12. getForm: () => FormActionType;
  13. };
  14. export function useTable(
  15. tableProps?: Props
  16. ): [(instance: TableActionType, formInstance: UseTableMethod) => void, TableActionType] {
  17. const tableRef = ref<Nullable<TableActionType>>(null);
  18. const loadedRef = ref<Nullable<boolean>>(false);
  19. const formRef = ref<Nullable<UseTableMethod>>(null);
  20. let stopWatch: WatchStopHandle;
  21. function register(instance: TableActionType, formInstance: UseTableMethod) {
  22. isProdMode() &&
  23. onUnmounted(() => {
  24. tableRef.value = null;
  25. loadedRef.value = null;
  26. });
  27. if (unref(loadedRef) && isProdMode() && instance === unref(tableRef)) return;
  28. tableRef.value = instance;
  29. formRef.value = formInstance;
  30. tableProps && instance.setProps(getDynamicProps(tableProps));
  31. loadedRef.value = true;
  32. stopWatch?.();
  33. stopWatch = watch(
  34. () => tableProps,
  35. () => {
  36. tableProps && instance.setProps(getDynamicProps(tableProps));
  37. },
  38. {
  39. immediate: true,
  40. deep: true,
  41. }
  42. );
  43. }
  44. function getTableInstance(): TableActionType {
  45. const table = unref(tableRef);
  46. if (!table) {
  47. error(
  48. 'The table instance has not been obtained yet, please make sure the table is presented when performing the table operation!'
  49. );
  50. }
  51. return table as TableActionType;
  52. }
  53. const methods: TableActionType & {
  54. getForm: () => FormActionType;
  55. } = {
  56. reload: async (opt?: FetchParams) => {
  57. getTableInstance().reload(opt);
  58. },
  59. setProps: (props: Partial<BasicTableProps>) => {
  60. getTableInstance().setProps(props);
  61. },
  62. redoHeight: () => {
  63. getTableInstance().redoHeight();
  64. },
  65. setLoading: (loading: boolean) => {
  66. getTableInstance().setLoading(loading);
  67. },
  68. getDataSource: () => {
  69. return toRaw(getTableInstance().getDataSource());
  70. },
  71. getColumns: ({ ignoreIndex = false }: { ignoreIndex?: boolean } = {}) => {
  72. const columns = getTableInstance().getColumns({ ignoreIndex }) || [];
  73. return toRaw(columns);
  74. },
  75. setColumns: (columns: BasicColumn[]) => {
  76. getTableInstance().setColumns(columns);
  77. },
  78. setTableData: (values: any[]) => {
  79. return getTableInstance().setTableData(values);
  80. },
  81. setPagination: (info: Partial<PaginationProps>) => {
  82. return getTableInstance().setPagination(info);
  83. },
  84. deleteSelectRowByKey: (key: string) => {
  85. getTableInstance().deleteSelectRowByKey(key);
  86. },
  87. getSelectRowKeys: () => {
  88. return toRaw(getTableInstance().getSelectRowKeys());
  89. },
  90. getSelectRows: () => {
  91. return toRaw(getTableInstance().getSelectRows());
  92. },
  93. clearSelectedRowKeys: () => {
  94. getTableInstance().clearSelectedRowKeys();
  95. },
  96. setSelectedRowKeys: (keys: string[] | number[]) => {
  97. getTableInstance().setSelectedRowKeys(keys);
  98. },
  99. getPaginationRef: () => {
  100. return getTableInstance().getPaginationRef();
  101. },
  102. getSize: () => {
  103. return toRaw(getTableInstance().getSize());
  104. },
  105. updateTableData: (index: number, key: string, value: any) => {
  106. return getTableInstance().updateTableData(index, key, value);
  107. },
  108. getRowSelection: () => {
  109. return toRaw(getTableInstance().getRowSelection());
  110. },
  111. getCacheColumns: () => {
  112. return toRaw(getTableInstance().getCacheColumns());
  113. },
  114. getForm: () => {
  115. return (unref(formRef) as unknown) as FormActionType;
  116. },
  117. setShowPagination: async (show: boolean) => {
  118. getTableInstance().setShowPagination(show);
  119. },
  120. getShowPagination: () => {
  121. return toRaw(getTableInstance().getShowPagination());
  122. },
  123. expandAll: () => {
  124. getTableInstance().expandAll();
  125. },
  126. collapseAll: () => {
  127. getTableInstance().collapseAll();
  128. },
  129. };
  130. return [register, methods];
  131. }