diff --git a/packages/s2-core/__tests__/unit/utils/sort-action-spec.ts b/packages/s2-core/__tests__/unit/utils/sort-action-spec.ts index 59ef2ac5ce..c687485fdb 100644 --- a/packages/s2-core/__tests__/unit/utils/sort-action-spec.ts +++ b/packages/s2-core/__tests__/unit/utils/sort-action-spec.ts @@ -324,6 +324,34 @@ describe('Sort By Func Tests', () => { '浙江[&]杭州', ]); }); + + // 测试自定义列头(对象数组)的场景 + test('should handle custom column headers (object array) correctly', () => { + const originValues = ['四川[&]成都', '四川[&]绵阳', '浙江[&]杭州']; + + const result = sortByFunc({ + originValues, + sortParam: { + sortFieldId: 'city', + sortFunc: () => ['绍兴', '绵阳', '杭州', '成都'], + }, + dataSet: { + fields: { + rows: ['province', 'city'], + // 使用自定义列头(对象数组) + columns: [ + { + field: 'type', + title: '类型', + }, + ], + }, + } as unknown as PivotDataSet, + }); + + // 应该正常返回排序结果,而不是因为自定义列头导致排序失效 + expect(result).toEqual(['四川[&]绵阳', '四川[&]成都', '浙江[&]杭州']); + }); }); describe('GetSortByMeasureValues Tests', () => { 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 6039cbb8da..6dd6591597 100644 --- a/packages/s2-core/src/data-set/pivot-data-set.ts +++ b/packages/s2-core/src/data-set/pivot-data-set.ts @@ -44,6 +44,7 @@ import type { ViewMetaData, } from '../common/interface'; import { Node } from '../facet/layout/node'; +import { getLeafColumnsWithKey } from '../facet/utils'; import { resolveNillString } from '../utils'; import { getAggregationAndCalcFuncByQuery } from '../utils/data-set-operate'; import { @@ -288,18 +289,21 @@ export class PivotDataSet extends BaseDataSet { sortedDimensionValues: string[], ) { const { rows, columns } = this.fields; + // 使用 getLeafColumnsWithKey 处理自定义列头(对象数组)的情况 + const rowFields = getLeafColumnsWithKey(rows); + const colFields = getLeafColumnsWithKey(columns); - if (includes(rows, sortFieldId)) { + if (rowFields.includes(sortFieldId)) { this.rowPivotMeta = getSortedPivotMeta({ pivotMeta: this.rowPivotMeta, - dimensions: rows as string[], + dimensions: rowFields, sortFieldId, sortedDimensionValues, }); - } else if (includes(columns, sortFieldId)) { + } else if (colFields.includes(sortFieldId)) { this.colPivotMeta = getSortedPivotMeta({ pivotMeta: this.colPivotMeta, - dimensions: columns as string[], + dimensions: colFields, sortFieldId, sortedDimensionValues, }); diff --git a/packages/s2-core/src/sheet-type/spread-sheet.ts b/packages/s2-core/src/sheet-type/spread-sheet.ts index 3b6f050f72..78c58f6bcb 100644 --- a/packages/s2-core/src/sheet-type/spread-sheet.ts +++ b/packages/s2-core/src/sheet-type/spread-sheet.ts @@ -61,6 +61,7 @@ import { Store } from '../common/store'; import type { BaseDataSet } from '../data-set'; import type { BaseFacet } from '../facet'; import type { Node } from '../facet/layout/node'; +import { getLeafColumnsWithKey } from '../facet/utils'; import { RootInteraction } from '../interaction/root'; import { getTheme } from '../theme'; import { HdAdapter } from '../ui/hd-adapter'; @@ -696,10 +697,12 @@ export abstract class SpreadSheet extends EE { public getTotalsConfig(dimension: string): Total { const { totals } = this.options; const { rows } = this.dataSet.fields; + // 使用 getLeafColumnsWithKey 处理自定义列头(对象数组)的情况 + const rowFields = getLeafColumnsWithKey(rows); const totalConfig = get( totals, - includes(rows, dimension) ? 'row' : 'col', + rowFields.includes(dimension) ? 'row' : 'col', {}, ) as Total; diff --git a/packages/s2-core/src/utils/sort-action.ts b/packages/s2-core/src/utils/sort-action.ts index da5ccf36b0..09fa404882 100644 --- a/packages/s2-core/src/utils/sort-action.ts +++ b/packages/s2-core/src/utils/sort-action.ts @@ -168,9 +168,13 @@ export const sortByFunc = (params: SortActionParams): string[] => { return originValues; } + // 使用 getLeafColumnsWithKey 处理自定义列头(对象数组)的情况 + const rowFields = getLeafColumnsWithKey(dataSet!.fields.rows); + const colFields = getLeafColumnsWithKey(dataSet!.fields.columns); + if ( - (dataSet!.fields.rows!.indexOf(sortFieldId) > 0 || - dataSet!.fields.columns!.indexOf(sortFieldId) > 0) && + (rowFields.indexOf(sortFieldId) > 0 || + colFields.indexOf(sortFieldId) > 0) && !includes(sortResult[0], NODE_ID_SEPARATOR) ) { /** @@ -192,7 +196,9 @@ const sortByMethod = (params: SortActionParams): string[] => { const { sortParam, measureValues, originValues, dataSet } = params; const { sortByMeasure, query, sortFieldId, sortMethod } = sortParam!; const { rows = [], columns = [] } = dataSet!.fields; - const isInRows = rows?.includes(sortFieldId); + // 使用 getLeafColumnsWithKey 处理自定义列头(对象数组)的情况 + const rowFields = getLeafColumnsWithKey(rows); + const isInRows = rowFields.includes(sortFieldId); let result; if (sortByMeasure) { @@ -203,7 +209,7 @@ const sortByMethod = (params: SortActionParams): string[] => { ); const fields = ( - isInRows ? rows : getLeafColumnsWithKey(columns) + isInRows ? rowFields : getLeafColumnsWithKey(columns) ) as string[]; result = getDimensionsWithParentPath( @@ -260,11 +266,11 @@ const createTotalParams = ( if (isMultipleDimensionValue) { // 获取行/列小计时,需要将所有行/列维度的值作为 params const realOriginValue = split(originValue, NODE_ID_SEPARATOR); - const currentFields = ( - fields?.rows?.includes(sortFieldId) - ? fields.rows - : getLeafColumnsWithKey(fields.columns) - ) as string[]; + // 使用 getLeafColumnsWithKey 处理自定义列头(对象数组)的情况 + const rowFields = getLeafColumnsWithKey(fields.rows); + const currentFields = rowFields.includes(sortFieldId) + ? rowFields + : getLeafColumnsWithKey(fields.columns); for (let i = 0; i <= indexOf(currentFields, sortFieldId); i++) { totalParams[currentFields[i]] = realOriginValue[i]; @@ -312,21 +318,21 @@ export const getSortByMeasureValues = ( // 按 query 查出所有数据 const columns = getLeafColumnsWithKey(fields.columns); + // 使用 getLeafColumnsWithKey 处理自定义列头(对象数组)的情况 + const rows = getLeafColumnsWithKey(fields.rows); /** * 按汇总值进行排序 - * 需要过滤出符合要求的 “汇总数据” + * 需要过滤出符合要求的 "汇总数据" * 因为 getCellMultiData 会查询出 query 及其子维度的所有数据 * 如 query={ type: 'xx' } 会包含 { type: 'xx', subType: '*' } 的数据 */ - const isSortFieldInRow = includes(fields.rows, sortFieldId); + const isSortFieldInRow = rows.includes(sortFieldId); // 排序字段所在一侧的全部字段 - const sortFields = filterExtraDimension( - isSortFieldInRow ? fields.rows : columns, - ); + const sortFields = filterExtraDimension(isSortFieldInRow ? rows : columns); // 与排序交叉的另一侧全部字段 const oppositeFields = filterExtraDimension( - isSortFieldInRow ? columns : fields.rows, + isSortFieldInRow ? columns : rows, ); const fieldAfterSortField = sortFields[sortFields.indexOf(sortFieldId) + 1];