|
1 |
| -export function useTable() {} |
| 1 | +import { ref, reactive } from 'vue'; |
| 2 | +import type { Ref } from 'vue'; |
| 3 | +import type { DataTableBaseColumn, DataTableSelectionColumn, DataTableExpandColumn, PaginationProps } from 'naive-ui'; |
| 4 | +import type { MaybeComputedRef } from '@vueuse/core'; |
| 5 | +import type { TableColumnGroup, InternalRowData } from 'naive-ui/es/data-table/src/interface'; |
| 6 | +import { useLoadingEmpty } from '../common'; |
| 7 | + |
| 8 | +/** |
| 9 | + * 表格的列 |
| 10 | + */ |
| 11 | +type DataTableColumn<T = InternalRowData> = |
| 12 | + | (Omit<TableColumnGroup<T>, 'key'> & { key: keyof T }) |
| 13 | + | (Omit<DataTableBaseColumn<T>, 'key'> & { key: keyof T }) |
| 14 | + | DataTableSelectionColumn<T> |
| 15 | + | DataTableExpandColumn<T>; |
| 16 | + |
| 17 | +/** |
| 18 | + * 表格分页参数 |
| 19 | + */ |
| 20 | +type TablePaginationParams = Pick<PaginationProps, 'page' | 'pageSize'>; |
| 21 | + |
| 22 | +/** |
| 23 | + * 表格接口的请求参数 |
| 24 | + */ |
| 25 | +type TableApiParams = Record<string, unknown> & TablePaginationParams; |
| 26 | + |
| 27 | +/** |
| 28 | + * 表格接口的请求数据 |
| 29 | + */ |
| 30 | +type TableApiData<T = InternalRowData> = { |
| 31 | + data: T[]; |
| 32 | + pageNum: number; |
| 33 | + pageSize: number; |
| 34 | + total: number; |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * 表格接口的请求函数 |
| 39 | + */ |
| 40 | +type TableApiFn<P extends TableApiParams, T extends InternalRowData> = ( |
| 41 | + params: P |
| 42 | +) => Promise<Service.SuccessResult<TableApiData<T>>>; |
| 43 | + |
| 44 | +export function useNaiveTable<TableData extends InternalRowData, P extends TableApiParams>( |
| 45 | + apiFn: TableApiFn<P, TableData>, |
| 46 | + apiParams: P, |
| 47 | + columns: MaybeComputedRef<DataTableColumn<TableData>[]> |
| 48 | +) { |
| 49 | + const { loading, startLoading, endLoading, empty, setEmpty } = useLoadingEmpty(); |
| 50 | + |
| 51 | + const tableData: Ref<TableData[]> = ref([]); |
| 52 | + |
| 53 | + async function getTableData(paginationParams?: TablePaginationParams) { |
| 54 | + startLoading(); |
| 55 | + |
| 56 | + const params = { ...apiParams, ...paginationParams }; |
| 57 | + |
| 58 | + const { data } = await apiFn(params); |
| 59 | + if (data) { |
| 60 | + tableData.value = data.data; |
| 61 | + |
| 62 | + setEmpty(data.data.length === 0); |
| 63 | + } |
| 64 | + endLoading(); |
| 65 | + } |
| 66 | + |
| 67 | + const pagination: PaginationProps = reactive({ |
| 68 | + page: 1, |
| 69 | + pageSize: 10, |
| 70 | + showSizePicker: true, |
| 71 | + pageSizes: [10, 15, 20, 25, 30], |
| 72 | + onChange: (page: number) => { |
| 73 | + pagination.page = page; |
| 74 | + }, |
| 75 | + onUpdatePageSize: (pageSize: number) => { |
| 76 | + pagination.pageSize = pageSize; |
| 77 | + pagination.page = 1; |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + return { |
| 82 | + tableData, |
| 83 | + columns, |
| 84 | + loading, |
| 85 | + empty, |
| 86 | + pagination, |
| 87 | + start: getTableData |
| 88 | + }; |
| 89 | +} |
0 commit comments