Skip to content

Commit f02c68a

Browse files
CopilotAlexzjt
andauthored
fix: 修复透视表组内排序在指标数据为 null 时排序异常的问题 (#3308)
* Initial plan * fix: 修复透视表组内排序在指标数据为 null 时排序异常的问题 Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com> * fix: 修复测试用例中 createCellData 函数的类型定义 Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com> * feat: 添加 nullsPlacement 配置项,支持自定义空值排序位置 Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com> * fix: 修正 nullsPlacement='auto' 的行为(升序空值在前,降序空值在后) Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com> * fix: 提供更完善的全局配置 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com> Co-authored-by: zjt <alex_zjt@163.com>
1 parent ea9c6a3 commit f02c68a

9 files changed

Lines changed: 392 additions & 20 deletions

File tree

‎packages/s2-core/__tests__/bugs/issue-725-spec.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('Group Sort When Have Same Child Measure', () => {
2424
sortFieldId: 'type',
2525
sortByMeasure: 'cost',
2626
sortMethod: 'asc',
27+
nullsPlacement: 'first',
2728
},
2829
],
2930
});

‎packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { PivotSheet, SpreadSheet } from '@/sheet-type';
2121
import { BaseTooltip } from '@/ui/tooltip';
2222
import { customMerge, setupDataConfig } from '@/utils';
2323
import { Canvas, CanvasEvent } from '@antv/g';
24-
import { cloneDeep, last } from 'lodash';
24+
import { cloneDeep, last, merge } from 'lodash';
2525
import dataCfg from 'tests/data/simple-data.json';
2626
import { waitForRender } from 'tests/util';
2727
import { createPivotSheet, getContainer, sleep } from 'tests/util/helpers';
@@ -1350,6 +1350,90 @@ describe('PivotSheet Tests', () => {
13501350

13511351
sheet.destroy();
13521352
});
1353+
1354+
// https://github.com/antvis/S2/issues/3308
1355+
it('should use previous defined nullsPlacement in sortParams', async () => {
1356+
const layoutDataCfg: S2DataConfig = customMerge(originalDataCfg, {
1357+
fields: {
1358+
rows: ['province'],
1359+
columns: [],
1360+
},
1361+
sortParams: [
1362+
{
1363+
sortFieldId: 'province',
1364+
sortMethod: 'DESC',
1365+
nullsPlacement: 'first',
1366+
},
1367+
],
1368+
} as unknown as S2DataConfig);
1369+
1370+
const sheet = new PivotSheet(getContainer(), layoutDataCfg, {
1371+
width: 400,
1372+
height: 200,
1373+
});
1374+
1375+
await sheet.render();
1376+
1377+
const provinceNode = sheet.facet.getRowNodes()[0];
1378+
1379+
// trigger group sort
1380+
await sheet.groupSortByMethod('ASC', provinceNode);
1381+
1382+
// should match previous defined nullsPlacement
1383+
expect(sheet.dataCfg.sortParams).toMatchObject([
1384+
{
1385+
sortFieldId: 'province',
1386+
sortMethod: 'ASC',
1387+
nullsPlacement: 'first',
1388+
},
1389+
]);
1390+
1391+
sheet.destroy();
1392+
});
1393+
1394+
it('should use wildcard defined nullsPlacement in sortParams', async () => {
1395+
const layoutDataCfg = merge({}, cloneDeep(originalDataCfg), {
1396+
fields: {
1397+
rows: ['province'],
1398+
columns: ['city'],
1399+
values: ['price'],
1400+
valueInCols: true,
1401+
},
1402+
sortParams: [
1403+
{
1404+
sortFieldId: '*',
1405+
nullsPlacement: 'first',
1406+
},
1407+
],
1408+
} as unknown as S2DataConfig);
1409+
1410+
const sheet = new PivotSheet(getContainer(), layoutDataCfg, {
1411+
width: 400,
1412+
height: 200,
1413+
});
1414+
1415+
await sheet.render();
1416+
1417+
const provinceNode = sheet.facet.getRowNodes()[0];
1418+
1419+
// trigger group sort
1420+
await sheet.groupSortByMethod('ASC', provinceNode);
1421+
1422+
// should match previous defined nullsPlacement
1423+
expect(sheet.dataCfg.sortParams).toMatchObject([
1424+
{
1425+
sortFieldId: '*',
1426+
nullsPlacement: 'first',
1427+
},
1428+
{
1429+
sortFieldId: 'city',
1430+
sortMethod: 'ASC',
1431+
nullsPlacement: 'first',
1432+
},
1433+
]);
1434+
1435+
sheet.destroy();
1436+
});
13531437
});
13541438

13551439
test('should emit destroy event', async () => {

‎packages/s2-core/__tests__/unit/utils/sort-action-spec.ts‎

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Sort Action Test', () => {
6464
});
6565

6666
test('sort action with object arr', () => {
67-
function createCellData(list: (number | string | undefined)[]) {
67+
function createCellData(list: (number | string | undefined | null)[]) {
6868
return list.map((a) => new CellData({ a }, 'a'));
6969
}
7070

@@ -101,13 +101,15 @@ describe('Sort Action Test', () => {
101101

102102
const data4 = createCellData(['-', 2, '3']);
103103

104+
// 默认 nullsPlacement='last',特殊值始终在最后
104105
expect(
105106
unwrapCellData(sortAction(data4, 'ASC', 'a') as CellData[]),
106-
).toEqual(['-', 2, '3']);
107+
).toEqual([2, '3', '-']);
107108
expect(
108109
unwrapCellData(sortAction(data4, 'DESC', 'a') as CellData[]),
109110
).toEqual(['3', 2, '-']);
110111

112+
// 默认 nullsPlacement='last',空值始终在最后
111113
expect(
112114
unwrapCellData(
113115
sortAction(
@@ -116,7 +118,7 @@ describe('Sort Action Test', () => {
116118
'a',
117119
) as CellData[],
118120
),
119-
).toEqual([undefined, '-', 2, '3']);
121+
).toEqual([2, '3', '-', undefined]);
120122
expect(
121123
unwrapCellData(
122124
sortAction(
@@ -127,15 +129,128 @@ describe('Sort Action Test', () => {
127129
),
128130
).toEqual(['3', 2, '-', undefined]);
129131

132+
// 测试 null 值排序,默认 nullsPlacement='last',无论升降序,null 都在最后
133+
const dataNullAsc = createCellData([null, 2, '3', 0]);
134+
135+
expect(
136+
unwrapCellData(sortAction(dataNullAsc, 'ASC', 'a') as CellData[]),
137+
).toEqual([0, 2, '3', null]);
138+
139+
const dataNullDesc = createCellData([null, 2, '3', 0]);
140+
141+
expect(
142+
unwrapCellData(sortAction(dataNullDesc, 'DESC', 'a') as CellData[]),
143+
).toEqual(['3', 2, 0, null]);
144+
145+
// 测试同时包含多个 null 值的情况(issue #3306 的场景)
146+
// 默认行为:null 始终在最后
147+
const dataMultiNull = createCellData([null, null, 0, 9, 0, -1]);
148+
const resultAsc = unwrapCellData(
149+
sortAction(dataMultiNull, 'ASC', 'a') as CellData[],
150+
);
151+
152+
// 升序时 null 值应该在最后面(默认 nullsPlacement='last')
153+
expect(resultAsc[4]).toBeNull();
154+
expect(resultAsc[5]).toBeNull();
155+
expect(resultAsc.slice(0, 4)).toEqual([-1, 0, 0, 9]);
156+
157+
const dataMultiNullDesc = createCellData([null, null, 0, 9, 0, -1]);
158+
const resultDesc = unwrapCellData(
159+
sortAction(dataMultiNullDesc, 'DESC', 'a') as CellData[],
160+
);
161+
162+
// 降序时 null 值应该在最后面(默认 nullsPlacement='last')
163+
expect(resultDesc[4]).toBeNull();
164+
expect(resultDesc[5]).toBeNull();
165+
expect(resultDesc.slice(0, 4)).toEqual([9, 0, 0, -1]);
166+
130167
const data6 = createCellData(['', 2, '3']);
131168

132169
expect(
133170
unwrapCellData(sortAction(data6, 'ASC', 'a') as CellData[]),
134-
).toEqual(['', 2, '3']);
171+
).toEqual([2, '3', '']);
135172
expect(
136173
unwrapCellData(sortAction(data6, 'DESC', 'a') as CellData[]),
137174
).toEqual(['3', 2, '']);
138175
});
176+
177+
test('sort action with nullsPlacement option', () => {
178+
function createCellData(list: (number | string | undefined | null)[]) {
179+
return list.map((a) => new CellData({ a }, 'a'));
180+
}
181+
182+
function unwrapCellData(cellDataList: CellData[]) {
183+
return cellDataList.map((cell) => cell[VALUE_FIELD]);
184+
}
185+
186+
const testData = [null, 2, '3', 0, undefined];
187+
188+
// nullsPlacement='last' (默认值) - 空值始终在最后
189+
expect(
190+
unwrapCellData(
191+
sortAction(
192+
createCellData(testData),
193+
'ASC',
194+
'a',
195+
'last',
196+
) as CellData[],
197+
),
198+
).toEqual([0, 2, '3', null, undefined]);
199+
expect(
200+
unwrapCellData(
201+
sortAction(
202+
createCellData(testData),
203+
'DESC',
204+
'a',
205+
'last',
206+
) as CellData[],
207+
),
208+
).toEqual(['3', 2, 0, null, undefined]);
209+
210+
// nullsPlacement='first' - 空值始终在最前
211+
expect(
212+
unwrapCellData(
213+
sortAction(
214+
createCellData(testData),
215+
'ASC',
216+
'a',
217+
'first',
218+
) as CellData[],
219+
),
220+
).toEqual([null, undefined, 0, 2, '3']);
221+
expect(
222+
unwrapCellData(
223+
sortAction(
224+
createCellData(testData),
225+
'DESC',
226+
'a',
227+
'first',
228+
) as CellData[],
229+
),
230+
).toEqual([null, undefined, '3', 2, 0]);
231+
232+
// nullsPlacement='auto' - 升序时空值在前,降序时空值在后
233+
expect(
234+
unwrapCellData(
235+
sortAction(
236+
createCellData(testData),
237+
'ASC',
238+
'a',
239+
'auto',
240+
) as CellData[],
241+
),
242+
).toEqual([null, undefined, 0, 2, '3']);
243+
expect(
244+
unwrapCellData(
245+
sortAction(
246+
createCellData(testData),
247+
'DESC',
248+
'a',
249+
'auto',
250+
) as CellData[],
251+
),
252+
).toEqual(['3', 2, 0, null, undefined]);
253+
});
139254
});
140255
});
141256

‎packages/s2-core/src/common/interface/basic.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ export interface Totals {
221221
col?: Total;
222222
}
223223

224+
/**
225+
* 空值排序位置
226+
* - 'first': 空值排在最前
227+
* - 'last': 空值排在最后
228+
* - 'auto': 升序时空值在前,降序时空值在后
229+
*/
230+
export type NullsPlacement = 'first' | 'last' | 'auto';
231+
224232
export interface Sort {
225233
/** 字段 id */
226234
sortFieldId: string;
@@ -239,6 +247,14 @@ export interface Sort {
239247

240248
/** 组内排序用来显示icon */
241249
type?: string;
250+
251+
/**
252+
* 空值排序位置
253+
* - 'first': 空值永远排在最前
254+
* - 'last': 空值永远排在最后(默认值)
255+
* - 'auto': 升序时空值在前,降序时空值在后
256+
*/
257+
nullsPlacement?: NullsPlacement;
242258
}
243259

244260
export interface SortFuncParam extends Sort {

‎packages/s2-core/src/sheet-type/pivot-sheet.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,20 @@ export class PivotSheet extends SpreadSheet {
161161
}
162162

163163
const sortFieldId = isString(sortField) ? sortField : sortField!.field;
164+
const currentSortParam = this.dataCfg.sortParams?.find(
165+
(item) =>
166+
item?.sortFieldId === sortFieldId || item?.sortFieldId === sortValue,
167+
);
168+
const wildcardSortParam = this.dataCfg.sortParams?.find(
169+
(item) => item?.sortFieldId === '*',
170+
);
164171
const sortParam: SortParam = {
165172
sortFieldId,
166173
sortMethod,
167174
sortByMeasure: sortValue,
168175
query: sortQuery,
176+
nullsPlacement:
177+
currentSortParam?.nullsPlacement ?? wildcardSortParam?.nullsPlacement,
169178
};
170179
const prevSortParams = this.dataCfg.sortParams?.filter(
171180
(item) => item?.sortFieldId !== sortField,

0 commit comments

Comments
 (0)