Skip to content

Commit

Permalink
fix: 修复指标不在最后一级配置与分组汇总同时使用时,获取数据为空
Browse files Browse the repository at this point in the history
  • Loading branch information
JuZe committed Oct 13, 2023
1 parent 406d17e commit dc1e93b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 9 deletions.
77 changes: 77 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 @@ -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: '纸张',
},
]);
});
});
14 changes: 5 additions & 9 deletions packages/s2-core/src/data-set/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit dc1e93b

Please sign in to comment.