fix: 表格排序翻页会丢失排序参数

This commit is contained in:
dap 2024-12-20 13:58:01 +08:00
parent 5e82866370
commit aa95783fc9
5 changed files with 41 additions and 7 deletions

View File

@ -8,6 +8,7 @@
**BUG FIXES**
- 字典项为空时getDict方法无限调用接口((无奈兼容 不给字典item本来就是错误用法))
- 表格排序翻页会丢失排序参数
# 1.1.3

View File

@ -4,6 +4,7 @@ import {
setupVbenVxeTable,
useVbenVxeGrid,
type VxeGridDefines,
type VxeGridPropTypes,
} from '@vben/plugins/vxe-table';
import { Button, Image } from 'ant-design-vue';
@ -133,6 +134,7 @@ export function vxeCheckboxChecked(
/**
* vxe-table排序事件 /
* @deprecated 使addSortParams代替
* @param tableApi api
* @param sortParams
*/
@ -151,3 +153,23 @@ export function vxeSortEvent(
const isAsc = sortList.map((item) => item.order).join(',');
tableApi.query({ orderByColumn, isAsc });
}
/**
*
* @param params
* @param sortList vxe-table的排序参数
*/
export function addSortParams(
params: Record<string, any>,
sortList: VxeGridPropTypes.ProxyAjaxQuerySortCheckedParams[],
) {
// 这里是排序取消 length为0 就不添加参数了
if (sortList.length === 0) {
return;
}
// 支持单/多字段排序
const orderByColumn = sortList.map((item) => item.field).join(',');
const isAsc = sortList.map((item) => item.order).join(',');
params.orderByColumn = orderByColumn;
params.isAsc = isAsc;
}

View File

@ -9,10 +9,10 @@ import { $t } from '@vben/locales';
import { Modal, Space } from 'ant-design-vue';
import {
addSortParams,
useVbenVxeGrid,
vxeCheckboxChecked,
type VxeGridProps,
vxeSortEvent,
} from '#/adapter/vxe-table';
import {
operLogClean,
@ -60,12 +60,14 @@ const gridOptions: VxeGridProps<OperationLog> = {
pagerConfig: {},
proxyConfig: {
ajax: {
query: async ({ page }, formValues = {}) => {
query: async ({ page, sorts }, formValues = {}) => {
const params: any = {
pageNum: page.currentPage,
pageSize: page.pageSize,
...formValues,
};
//
addSortParams(params, sorts);
return await operLogList(params);
},
},
@ -87,7 +89,8 @@ const [BasicTable, tableApi] = useVbenVxeGrid({
formOptions,
gridOptions,
gridEvents: {
sortChange: (sortParams) => vxeSortEvent(tableApi, sortParams),
//
sortChange: () => tableApi.query(),
},
});

View File

@ -19,10 +19,10 @@ import {
} from 'ant-design-vue';
import {
addSortParams,
useVbenVxeGrid,
vxeCheckboxChecked,
type VxeGridProps,
vxeSortEvent,
} from '#/adapter/vxe-table';
import { configInfoByKey } from '#/api/system/config';
import { ossDownload, ossList, ossRemove } from '#/api/system/oss';
@ -66,12 +66,14 @@ const gridOptions: VxeGridProps = {
pagerConfig: {},
proxyConfig: {
ajax: {
query: async ({ page }, formValues = {}) => {
query: async ({ page, sorts }, formValues = {}) => {
const params: any = {
pageNum: page.currentPage,
pageSize: page.pageSize,
...formValues,
};
//
addSortParams(params, sorts);
return await ossList(params);
},
},
@ -94,7 +96,8 @@ const [BasicTable, tableApi] = useVbenVxeGrid({
formOptions,
gridOptions,
gridEvents: {
sortChange: (sortParams) => vxeSortEvent(tableApi, sortParams),
//
sortChange: () => tableApi.query(),
},
});

View File

@ -2,4 +2,9 @@ export { setupVbenVxeTable } from './init';
export type { VxeTableGridOptions } from './types';
export * from './use-vxe-grid';
export { default as VbenVxeGrid } from './use-vxe-grid.vue';
export type { VxeGridDefines, VxeGridListeners, VxeGridProps } from 'vxe-table';
export type {
VxeGridDefines,
VxeGridListeners,
VxeGridProps,
VxeGridPropTypes,
} from 'vxe-table';