Skip to content

Commit

Permalink
feat(projects): mock manage list data with pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Feb 25, 2024
1 parent c7e2c55 commit 1a6be00
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
16 changes: 15 additions & 1 deletion src/hooks/common/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ export function useTable<TableData extends BaseData, Fn extends ApiFn, CustomCol
async function getData() {
startLoading();

const response = await apiFn(searchParams);
const formattedParams = formatSearchParams(searchParams);

const response = await apiFn(formattedParams);

const { data: tableData, pageNum, pageSize, total } = transformer(response as Awaited<ReturnType<Fn>>);

Expand All @@ -115,6 +117,18 @@ export function useTable<TableData extends BaseData, Fn extends ApiFn, CustomCol
endLoading();
}

function formatSearchParams(params: Record<string, unknown>) {
const formattedParams: Record<string, unknown> = {};

Object.entries(params).forEach(([key, value]) => {
if (value !== null && value !== undefined) {
formattedParams[key] = value;
}
});

return formattedParams;
}

/**
* update search params
*
Expand Down
2 changes: 1 addition & 1 deletion src/router/elegant/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const generatedRoutes: GeneratedRoute[] = [
meta: {
title: 'function_hide-child',
i18nKey: 'route.function_hide-child',
icon: 'material-symbols:filter-list-off',
icon: 'material-symbols:filter-list-off'
},
redirect: '/function/hide-child/one',
children: [
Expand Down
27 changes: 22 additions & 5 deletions src/views/manage/role/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import RoleSearch from './modules/role-search.vue';
const appStore = useAppStore();
const { bool: drawerVisible, setTrue: openDrawer } = useBoolean();
const { columns, filteredColumns, data, loading, pagination, getData, searchParams, resetSearchParams } = useTable<
Api.SystemManage.Role,
typeof fetchGetRoleList,
'index' | 'operate'
>({
const {
columns,
filteredColumns,
data,
loading,
pagination,
getData,
searchParams,
updateSearchParams,
resetSearchParams
} = useTable<Api.SystemManage.Role, typeof fetchGetRoleList, 'index' | 'operate'>({
apiFn: fetchGetRoleList,
apiParams: {
current: 1,
Expand All @@ -38,6 +44,16 @@ const { columns, filteredColumns, data, loading, pagination, getData, searchPara
total
};
},
onPaginationChanged(pg) {
const { page, pageSize } = pg;
updateSearchParams({
current: page,
size: pageSize
});
getData();
},
columns: () => [
{
type: 'selection',
Expand Down Expand Up @@ -179,6 +195,7 @@ function getIndex(index: number) {
:flex-height="!appStore.isMobile"
:scroll-x="702"
:loading="loading"
remote
:pagination="pagination"
:row-key="item => item.id"
class="sm:h-full"
Expand Down
27 changes: 22 additions & 5 deletions src/views/manage/user/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import UserSearch from './modules/user-search.vue';
const appStore = useAppStore();
const { bool: drawerVisible, setTrue: openDrawer } = useBoolean();
const { columns, filteredColumns, data, loading, pagination, getData, searchParams, resetSearchParams } = useTable<
Api.SystemManage.User,
typeof fetchGetUserList,
'index' | 'operate'
>({
const {
columns,
filteredColumns,
data,
loading,
pagination,
getData,
searchParams,
updateSearchParams,
resetSearchParams
} = useTable<Api.SystemManage.User, typeof fetchGetUserList, 'index' | 'operate'>({
apiFn: fetchGetUserList,
apiParams: {
current: 1,
Expand All @@ -41,6 +47,16 @@ const { columns, filteredColumns, data, loading, pagination, getData, searchPara
total
};
},
onPaginationChanged(pg) {
const { page, pageSize } = pg;
updateSearchParams({
current: page,
size: pageSize
});
getData();
},
columns: () => [
{
type: 'selection',
Expand Down Expand Up @@ -209,6 +225,7 @@ function getIndex(index: number) {
:flex-height="!appStore.isMobile"
:scroll-x="962"
:loading="loading"
remote
:pagination="pagination"
:row-key="item => item.id"
class="sm:h-full"
Expand Down

1 comment on commit 1a6be00

@dodu2014
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍,试了下,确实 remote 可以的,昨天提交的pr #331 中还有一项,pagination 的 onChange api 已经被官方标记为过时,建议使用 onUpdatePage

Please sign in to comment.