-
Notifications
You must be signed in to change notification settings - Fork 202
Pivot: preserve expanded rows when adding a field, sorting, or refreshing #9785
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
base: main
Are you sure you want to change the base?
Changes from all commits
7d3aca1
88ad191
c35cd9a
b02c3df
7d55093
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| buildExpandKey, | ||
| childExpandKey, | ||
| expandKeyDepth, | ||
| expandKeySegments, | ||
| parentExpandKey, | ||
| } from "./pivot-expand-keys"; | ||
|
|
||
| describe("pivot-expand-keys", () => { | ||
| it("round-trips a value path through build/segments", () => { | ||
| const values = ["Galatea-Stories", "facebook", "Jul 2026"]; | ||
| const key = buildExpandKey(values); | ||
| expect(expandKeySegments(key)).toEqual(values); | ||
| expect(expandKeyDepth(key)).toBe(3); | ||
| }); | ||
|
|
||
| it("keeps values that contain dots or spaces intact", () => { | ||
| const values = ["a.b.c", "x y z"]; | ||
| const key = buildExpandKey(values); | ||
| expect(expandKeySegments(key)).toEqual(values); | ||
| expect(expandKeyDepth(key)).toBe(2); | ||
| }); | ||
|
|
||
| it("encodes null distinctly from a genuinely absent level", () => { | ||
| const withNull = buildExpandKey(["app", null]); | ||
| const shallow = buildExpandKey(["app"]); | ||
| expect(withNull).not.toBe(shallow); | ||
| expect(expandKeyDepth(withNull)).toBe(2); | ||
| expect(expandKeyDepth(shallow)).toBe(1); | ||
| }); | ||
|
|
||
| it("computes the parent by stripping the deepest segment", () => { | ||
| const key = buildExpandKey(["app", "src", "month"]); | ||
| const parent = parentExpandKey(key); | ||
| expect(expandKeySegments(parent)).toEqual(["app", "src"]); | ||
| expect(parentExpandKey(buildExpandKey(["app"]))).toBe(""); | ||
| expect(parentExpandKey("")).toBe(""); | ||
| }); | ||
|
|
||
| it("childExpandKey extends a parent and is inverse of parentExpandKey", () => { | ||
| const parent = buildExpandKey(["app", "src"]); | ||
| const child = childExpandKey(parent, "month"); | ||
| expect(expandKeySegments(child)).toEqual(["app", "src", "month"]); | ||
| expect(parentExpandKey(child)).toBe(parent); | ||
| expect(childExpandKey("", "app")).toBe(buildExpandKey(["app"])); | ||
| }); | ||
|
|
||
| it("treats the empty root key as depth 0", () => { | ||
| expect(expandKeyDepth("")).toBe(0); | ||
| expect(expandKeySegments("")).toEqual([]); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * Value-based keys for pivot row expansion. A row is keyed by the dimension | ||
| * values from the root to it, NUL-joined and hierarchical, so the key stays | ||
| * stable across sorting, adding a field, and data refreshes. Uses the same | ||
| * separator and null sentinel as the dimKeys in pivot-click-selection.ts, | ||
| * except that undefined also maps to the sentinel here. | ||
| */ | ||
|
|
||
| // NUL separator, since dimension values won't contain it. | ||
| export const EXPAND_KEY_SEP = "\0"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consumers of the old dot-path row ids were not migrated to this separator. |
||
|
|
||
| // Sentinel for null values, so a null at depth N differs from an absent level. | ||
| // A literal "<NULL>" string value encodes the same way, which only matters if | ||
| // both appear under the same parent; the dimKeys make the same trade-off. | ||
| const NULL_SENTINEL = "<NULL>"; | ||
|
|
||
| // The leading separator can't occur in a real id, so this can't collide. | ||
| export const PIVOT_TOTALS_ROW_ID = EXPAND_KEY_SEP + "totals"; | ||
|
|
||
| export function encodeExpandKeyValue(value: unknown): string { | ||
| return value === null || value === undefined ? NULL_SENTINEL : String(value); | ||
| } | ||
|
|
||
| export function buildExpandKey(values: unknown[]): string { | ||
| return values.map(encodeExpandKeyValue).join(EXPAND_KEY_SEP); | ||
| } | ||
|
|
||
| export function expandKeySegments(key: string): string[] { | ||
| return key === "" ? [] : key.split(EXPAND_KEY_SEP); | ||
| } | ||
|
|
||
| export function expandKeyDepth(key: string): number { | ||
| return expandKeySegments(key).length; | ||
| } | ||
|
|
||
| export function parentExpandKey(key: string): string { | ||
| const i = key.lastIndexOf(EXPAND_KEY_SEP); | ||
| return i === -1 ? "" : key.slice(0, i); | ||
| } | ||
|
|
||
| export function childExpandKey(parentKey: string, value: unknown): string { | ||
| const segment = encodeExpandKeyValue(value); | ||
| return parentKey === "" ? segment : parentKey + EXPAND_KEY_SEP + segment; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Three sibling totals-row checks still compare against the positional id
"0", whichgetRowIdno longer returns:PivotTable.svelte:391,FlatTable.svelte:245andNestedTable.svelte:570. InPivotTable.sveltea canvas click on a totals-row measure cell is therefore no longer short-circuited and falls through toonCellClickToFilter;getValuesForExpandedKeyresolves no row values for"\0totals", sogetFiltersForCellapplies a filter built from the column entries alone. In the two table componentsisTotalsRowis false for the totals row, sogetCellFormattingstops returning null and totals cells pick up conditional-formatting colours, andflatCellState/nestedCellStatemark theminteractiveCellwhen click-to-filter is enabled.