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: 修复树状模式下,总计小计格式化问题 #2219

Merged
merged 5 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/s2-core/__tests__/bugs/issue-1624-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ describe('Data Cell Border Tests', () => {

await sleep(40);

// @ts-ignore
const meta = dataCell.getCellArea();
const borderBbox = dataCell.getChildByIndex(2).getBBox();
// @ts-ignore
const borderBbox = dataCell.stateShapes
.get('interactiveBorderShape')
.getBBox();

expect(meta.x).toEqual(borderBbox.x - borderWidth / 2);
expect(meta.y).toEqual(borderBbox.y - borderWidth / 2);
Expand Down
35 changes: 35 additions & 0 deletions packages/s2-core/__tests__/unit/cell/header-cell-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,41 @@ describe('header cell formatter test', () => {
expect(rowCell.getFieldValue()).toBe('小计');
});

test('should not format pivot row grand total cell in tree mode', () => {
const rowGrandTotalNode = new Node({
id: `root[&]总计`,
key: '',
value: '总计',
parent: root,
label: '总计',
isTotals: true,
isGrandTotals: true,
});

const rowSubTotalNode = new Node({
id: `root[&]杭州`,
key: '',
value: '杭州',
parent: root,
label: '杭州',
isTotals: true,
isSubTotals: true,
isGrandTotals: false,
});

const formatter: Formatter = (value) => {
return `${value}1`;
};
jest.spyOn(s2.dataSet, 'getFieldFormatter').mockReturnValue(formatter);
jest.spyOn(s2, 'isHierarchyTreeType').mockReturnValue(true);

const grandTotalCell = new ColCell(rowGrandTotalNode, s2);
const subTotalCell = new RowCell(rowSubTotalNode, s2);

expect(grandTotalCell.getFieldValue()).toBe('总计');
expect(subTotalCell.getFieldValue()).toBe('杭州1');
});

test('pivot corner cell not formatter', () => {
const formatter: Formatter = (value) => {
return `${value}1`;
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/cell/data-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ export class DataCell extends BaseCell<ViewMeta> {
this.resetTextAndConditionIconShapes();
this.drawBackgroundShape();
this.drawInteractiveBgShape();
this.drawInteractiveBorderShape();
if (!this.shouldHideRowSubtotalData()) {
this.drawConditionIntervalShape();
this.drawTextShape();
this.drawConditionIconShapes();
}
this.drawInteractiveBorderShape();
wjgogogo marked this conversation as resolved.
Show resolved Hide resolved
if (this.meta.isFrozenCorner) {
this.drawBorderShape();
}
Expand Down
18 changes: 14 additions & 4 deletions packages/s2-core/src/cell/header-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,27 @@ export abstract class HeaderCell extends BaseCell<Node> {
}

protected getFormattedFieldValue(): FormatResult {
const { label, isTotals } = this.meta;
const { label, isTotals, isGrandTotals } = this.meta;

const formatter = this.spreadsheet.dataSet.getFieldFormatter(
this.meta.field,
);

// 如果是 总计/小计 文字单元格,不需要被格式化
// 如果是 table mode,列头不需要被格式化
const isTableMode = this.spreadsheet.isTableMode();
// 树状模式下,小计是父维度本身,需要被格式化,此时只有总计才不需要被格式化
// 平铺模式下,总计/小计 文字单元格,不需要被格式化
// 自定义树模式下,没有总计小计概念,isTotals 均为 false, 所以不受影响
let shouldFormat = true;
if (this.spreadsheet.isTableMode()) {
shouldFormat = false;
} else if (this.spreadsheet.isHierarchyTreeType()) {
shouldFormat = !isGrandTotals;
} else {
shouldFormat = !isTotals;
}

const formattedValue =
formatter && !isTableMode && !isTotals
shouldFormat && formatter
? formatter(label, undefined, this.meta)
: label;

Expand Down