-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f748f2
commit cc13fcc
Showing
2 changed files
with
130 additions
and
42 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,89 @@ | ||
export function useTable() {} | ||
import { ref, reactive } from 'vue'; | ||
import type { Ref } from 'vue'; | ||
import type { DataTableBaseColumn, DataTableSelectionColumn, DataTableExpandColumn, PaginationProps } from 'naive-ui'; | ||
import type { MaybeComputedRef } from '@vueuse/core'; | ||
import type { TableColumnGroup, InternalRowData } from 'naive-ui/es/data-table/src/interface'; | ||
import { useLoadingEmpty } from '../common'; | ||
|
||
/** | ||
* 表格的列 | ||
*/ | ||
type DataTableColumn<T = InternalRowData> = | ||
| (Omit<TableColumnGroup<T>, 'key'> & { key: keyof T }) | ||
| (Omit<DataTableBaseColumn<T>, 'key'> & { key: keyof T }) | ||
| DataTableSelectionColumn<T> | ||
| DataTableExpandColumn<T>; | ||
|
||
/** | ||
* 表格分页参数 | ||
*/ | ||
type TablePaginationParams = Pick<PaginationProps, 'page' | 'pageSize'>; | ||
|
||
/** | ||
* 表格接口的请求参数 | ||
*/ | ||
type TableApiParams = Record<string, unknown> & TablePaginationParams; | ||
|
||
/** | ||
* 表格接口的请求数据 | ||
*/ | ||
type TableApiData<T = InternalRowData> = { | ||
data: T[]; | ||
pageNum: number; | ||
pageSize: number; | ||
total: number; | ||
}; | ||
|
||
/** | ||
* 表格接口的请求函数 | ||
*/ | ||
type TableApiFn<P extends TableApiParams, T extends InternalRowData> = ( | ||
params: P | ||
) => Promise<Service.SuccessResult<TableApiData<T>>>; | ||
|
||
export function useNaiveTable<TableData extends InternalRowData, P extends TableApiParams>( | ||
apiFn: TableApiFn<P, TableData>, | ||
apiParams: P, | ||
columns: MaybeComputedRef<DataTableColumn<TableData>[]> | ||
) { | ||
const { loading, startLoading, endLoading, empty, setEmpty } = useLoadingEmpty(); | ||
|
||
const tableData: Ref<TableData[]> = ref([]); | ||
|
||
async function getTableData(paginationParams?: TablePaginationParams) { | ||
startLoading(); | ||
|
||
const params = { ...apiParams, ...paginationParams }; | ||
|
||
const { data } = await apiFn(params); | ||
if (data) { | ||
tableData.value = data.data; | ||
|
||
setEmpty(data.data.length === 0); | ||
} | ||
endLoading(); | ||
} | ||
|
||
const pagination: PaginationProps = reactive({ | ||
page: 1, | ||
pageSize: 10, | ||
showSizePicker: true, | ||
pageSizes: [10, 15, 20, 25, 30], | ||
onChange: (page: number) => { | ||
pagination.page = page; | ||
}, | ||
onUpdatePageSize: (pageSize: number) => { | ||
pagination.pageSize = pageSize; | ||
pagination.page = 1; | ||
} | ||
}); | ||
|
||
return { | ||
tableData, | ||
columns, | ||
loading, | ||
empty, | ||
pagination, | ||
start: getTableData | ||
}; | ||
} |