Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复分组汇总时,按汇总排序获取排序数据为空 #2370

Merged
merged 11 commits into from
Oct 20, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GetSortByMeasureValues Total Fallback Tests should sort by col total whit group 1`] = `
Array [
Object {
"$$extra$$": "price",
"$$value$$": "666",
"city": "杭州",
"price": "666",
"type": "笔",
},
Object {
"$$extra$$": "price",
"$$value$$": "999",
"city": "杭州",
"price": "999",
"type": "纸张",
},
]
`;
52 changes: 52 additions & 0 deletions packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -714,4 +714,56 @@ describe('GetSortByMeasureValues Total Fallback Tests', () => {
},
]);
});

test('should sort by col total whit group', () => {
const currentOptions = {
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'],
},
};
sheet = new PivotSheet(getContainer(), dataConfig, currentOptions);
sheet.render();
// 根据列(类别)的总和排序
const sortParam: SortParam = {
sortFieldId: 'type',
sortByMeasure: TOTAL_VALUE,
sortMethod: 'desc',
query: {
[EXTRA_FIELD]: 'price',
city: '杭州',
},
};

const params: SortActionParams = {
dataSet: sheet.dataSet,
sortParam,
};
const measureValues = getSortByMeasureValues(params);
expect(measureValues).toMatchSnapshot();
});
});
2 changes: 2 additions & 0 deletions packages/s2-core/src/data-set/base-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,14 @@ export abstract class BaseDataSet {
* @param isTotals
* @param isRow
* @param drillDownFields
* @param withMissedField 用于标记是否需要汇总数据混入其中,在排序功能中使用
*/
public abstract getMultiData(
query: DataType,
isTotals?: boolean,
isRow?: boolean,
drillDownFields?: string[],
withMissedField?: boolean,
lijinke666 marked this conversation as resolved.
Show resolved Hide resolved
): DataType[];

public moreThanOneValue() {
Expand Down
12 changes: 6 additions & 6 deletions packages/s2-core/src/data-set/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,25 +570,23 @@
* 补足分组汇总场景的前置 undefined
* {undefined,'可乐','undefined','price'}
* => [
* {'可口公司','可乐','undefined','price'},

Check warning on line 573 in packages/s2-core/src/data-set/pivot-data-set.ts

View check run for this annotation

Codecov / codecov/patch

packages/s2-core/src/data-set/pivot-data-set.ts#L573

Added line #L573 was not covered by tests
* {'百事公司','可乐','undefined','price'},
* ]
*/
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;
}
} else if (existDimensionGroupKey) {
const allCurrentFieldDimensionValues =

Check warning on line 587 in packages/s2-core/src/data-set/pivot-data-set.ts

View check run for this annotation

Codecov / codecov/patch

packages/s2-core/src/data-set/pivot-data-set.ts#L587

Added line #L587 was not covered by tests
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) {
Expand All @@ -601,7 +599,7 @@
})
) {
const arrTypeValue = dimValue.split(ID_SEPARATOR);
const currentKey = arrTypeValue[arrayLength - 2];
const currentKey = arrTypeValue[i];
if (currentKey !== 'undefined') {
resKeys.push(currentKey);
}
Expand Down Expand Up @@ -662,6 +660,7 @@
isTotals?: boolean,
isRow?: boolean,
drillDownFields?: string[],
withMissedField?: boolean,
lijinke666 marked this conversation as resolved.
Show resolved Hide resolved
): DataType[] {
if (isEmpty(query)) {
return compact(customFlattenDeep(this.indexesData));
Expand All @@ -672,7 +671,8 @@
: rows;
// existDimensionGroup:当 undefined 维度后面有非 undefined,为维度分组场景,将非 undefined 维度前的维度填充为所有可能的维度值。
// 如 [undefined , '杭州市' , undefined , 'number']
const existDimensionGroup = this.checkExistDimensionGroup(query);
const existDimensionGroup =
!withMissedField && this.checkExistDimensionGroup(query);
NoobNotN marked this conversation as resolved.
Show resolved Hide resolved
let result = [];
if (existDimensionGroup) {
result = this.getGroupTotalMultiData(totalRows, query);
Expand Down
8 changes: 7 additions & 1 deletion packages/s2-core/src/utils/sort-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ export const getSortByMeasureValues = (
const { dataSet, sortParam, originValues } = params;
const { fields } = dataSet;
const { sortByMeasure, query, sortFieldId } = sortParam;
const dataList = dataSet.getMultiData(query); // 按 query 查出所有数据
const dataList = dataSet.getMultiData(
query,
undefined,
undefined,
undefined,
true,
); // 按 query 查出所有数据
const columns = getLeafColumnsWithKey(fields.columns);
/**
* 按明细数据
Expand Down
Loading