diff --git a/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx b/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx index 5b90bdb267..da9b75f336 100644 --- a/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx +++ b/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx @@ -715,3 +715,80 @@ describe('GetSortByMeasureValues Total Fallback Tests', () => { ]); }); }); +describe('GetSortByMeasureValues Total With Group Fallback Tests', () => { + let sheet: PivotSheet; + let dataSet: PivotDataSet; + const s2Options = { + totals: { + col: { + totalsGroupDimensions: ['city'], + showGrandTotals: true, + }, + }, + } as S2Options; + + const dataConfig = { + ...sortData, + data: [ + ...sortData.data, + { + city: '杭州', + type: '纸张', + price: '999', + }, + { + city: '杭州', + type: '笔', + price: '666', + }, + ], + fields: { + rows: ['type'], + columns: ['province', 'city'], + values: ['price'], + }, + }; + + beforeEach(() => { + sheet = new PivotSheet(getContainer(), dataConfig, s2Options); + dataSet = new PivotDataSet(sheet); + dataSet.setDataCfg(dataConfig); + sheet.dataSet = dataSet; + }); + + test('should sort by col total whit group', () => { + sheet.render(); + // 根据列(类别)的总和排序 + const sortParam: SortParam = { + sortFieldId: 'type', + sortByMeasure: TOTAL_VALUE, + sortMethod: 'desc', + query: { + [EXTRA_FIELD]: 'price', + city: '杭州', + }, + }; + + const params: SortActionParams = { + dataSet, + sortParam, + }; + const measureValues = getSortByMeasureValues(params); + expect(measureValues).toEqual([ + { + $$extra$$: 'price', + $$value$$: '666', + city: '杭州', + price: '666', + type: '笔', + }, + { + $$extra$$: 'price', + $$value$$: '999', + city: '杭州', + price: '999', + type: '纸张', + }, + ]); + }); +}); diff --git a/packages/s2-core/src/data-set/pivot-data-set.ts b/packages/s2-core/src/data-set/pivot-data-set.ts index 545d9eebf8..fc11dedd40 100644 --- a/packages/s2-core/src/data-set/pivot-data-set.ts +++ b/packages/s2-core/src/data-set/pivot-data-set.ts @@ -577,8 +577,8 @@ export class PivotDataSet extends BaseDataSet { getTotalGroupQueries(dimensions: string[], originQuery: DataType) { let queries = [originQuery]; let existDimensionGroupKey = null; - for (let i = dimensions.length; i > 0; i--) { - const key = dimensions[i - 1]; + for (let i = dimensions.length - 1; i >= 0; i--) { + const key = dimensions[i]; if (keys(originQuery).includes(key)) { if (key !== EXTRA_FIELD) { existDimensionGroupKey = key; @@ -587,8 +587,6 @@ export class PivotDataSet extends BaseDataSet { const allCurrentFieldDimensionValues = this.sortedDimensionValues[existDimensionGroupKey]; let res = []; - const arrayLength = - allCurrentFieldDimensionValues[0].split(ID_SEPARATOR).length; for (const query of queries) { const resKeys = []; for (const dimValue of allCurrentFieldDimensionValues) { @@ -601,14 +599,12 @@ export class PivotDataSet extends BaseDataSet { }) ) { const arrTypeValue = dimValue.split(ID_SEPARATOR); - const currentKey = arrTypeValue[arrayLength - 2]; - if (currentKey !== 'undefined') { - resKeys.push(currentKey); - } + const currentKey = arrTypeValue[i]; + resKeys.push(currentKey); } } const queryList = uniq(resKeys).map((v) => { - return { ...query, [key]: v }; + return { ...query, [key]: v === 'undefined' ? undefined : v }; }); res = concat(res, queryList); }