Skip to content

Commit

Permalink
fix(layout): 修复空数据的情况开启汇总分组渲染报错 close #2661 (#2662)
Browse files Browse the repository at this point in the history
fix(layout): 修复空数组的情况开启汇总分组渲染报错 close #2661
  • Loading branch information
lijinke666 committed Apr 26, 2024
1 parent 323f14d commit 8158660
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
38 changes: 38 additions & 0 deletions packages/s2-core/__tests__/spreadsheet/total-group-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,42 @@ describe('Total Group Dimension Test', () => {
cost: 9999,
});
});

// https://github.com/antvis/S2/issues/2661
test.each([
{ totalsGroupDimensions: [] },
{ totalsGroupDimensions: ['city'] },
])(
'should render correctly group totals layout if data is empty by %o',
async (config) => {
s2 = new PivotSheet(container, dataCfg, {
totals: {
...config,
col: {
showGrandTotals: true,
showSubTotals: true,
reverseGrandTotalsLayout: true,
reverseSubTotalsLayout: true,
},
},
});

s2.setDataCfg({
fields: {
rows: ['type'],
columns: ['province', 'city'],
values: ['price', 'cost'],
},
data: [],
});
await s2.render();

const { colNodes, colsHierarchy } = s2.facet.getLayoutResult();

expect(colsHierarchy.height).toEqual(90);
expect(colNodes).toHaveLength(4);
expect(colNodes.find((node) => node.value === '总计')).toBeFalsy();
expect(colNodes.find((node) => node.value === '小计')).toBeFalsy();
},
);
});
7 changes: 4 additions & 3 deletions packages/s2-core/src/facet/layout/build-gird-hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ const buildTotalGridHierarchy = (params: GridHeaderParams) => {

let query: Record<string, unknown> = {};
const totalsConfig = spreadsheet.getTotalsConfig(currentField);
const dimensionGroup = parentNode.isGrandTotals
? totalsConfig.grandTotalsGroupDimensions
: totalsConfig.subTotalsGroupDimensions;
const defaultDimensionGroup = parentNode.isGrandTotals
? totalsConfig.grandTotalsGroupDimensions || []
: totalsConfig.subTotalsGroupDimensions || [];
const dimensionGroup = !dataSet.isEmpty() ? defaultDimensionGroup : [];

if (dimensionGroup?.includes(currentField)) {
query = getDimsCondition(parentNode);
Expand Down
4 changes: 3 additions & 1 deletion packages/s2-core/src/facet/pivot-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,11 @@ export class PivotFacet extends FrozenFacet {
const { rows, columns } = dataSet.fields;
const fields = isRowHeader ? rows : columns;
const totalConfig = isRowHeader ? totals!.row : totals!.col;
const dimensionGroup = isSubTotal
const defaultDimensionGroup = isSubTotal
? totalConfig?.subTotalsGroupDimensions || []
: totalConfig?.grandTotalsGroupDimensions || [];
const dimensionGroup = !dataSet.isEmpty() ? defaultDimensionGroup : [];

const multipleMap: number[] = Array.from({ length: maxLevel + 1 }, () => 1);

for (let level = maxLevel; level > 0; level--) {
Expand Down

0 comments on commit 8158660

Please sign in to comment.