Skip to content

Commit

Permalink
[FIX] pivot: sorting of undefined headers
Browse files Browse the repository at this point in the history
Steps to reproduce:
* Create a pivot table with headers value "Alice", "Olaf", "".

=> The headers are sorted in the following order: "Alice", "(Undefined)", "Olaf".

The root cause was that the sorting function sorts the keys of the
groupby, and the key of the undefined object is the string "null" and so
it was sorted between the A of Alice and the O of Olaf.

closes #4898

Task: 4123261
X-original-commit: fc7eb95
Signed-off-by: Rémi Rahir (rar) <[email protected]>
  • Loading branch information
pro-odoo authored and rrahir committed Aug 23, 2024
1 parent 0529c30 commit b2e40ea
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ function orderDataEntriesKeys(
* Used to order two values
*/
function compareDimensionValues(dimension: PivotDimension, a: string, b: string): number {
if (a === "null") {
return dimension.order === "asc" ? 1 : -1;
}
if (b === "null") {
return dimension.order === "asc" ? -1 : 1;
}
if (dimension.type === "integer" || dimension.type === "date") {
return dimension.order === "asc" ? Number(a) - Number(b) : Number(b) - Number(a);
}
Expand Down
48 changes: 48 additions & 0 deletions tests/pivots/spreadsheet_pivot/spreadsheet_pivot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,54 @@ describe("Spreadsheet Pivot", () => {
expect(model.getters.getPivot("1").definition.rows[0].order).toBeUndefined();
});

test("Order of undefined value is correct when ordered asc", () => {
// prettier-ignore
const grid = {
A1: "Customer", B1: "Price", C1: "=PIVOT(1)",
A2: "Alice", B2: "10",
A3: "", B3: "20",
A4: "Olaf", B4: "30",
};
const model = createModelFromGrid(grid);
addPivot(model, "A1:B4", {
rows: [{ fieldName: "Customer", order: "asc" }],
columns: [],
measures: [{ id: "price", fieldName: "Price", aggregator: "sum" }],
});
// prettier-ignore
expect(getEvaluatedGrid(model, "C1:C5")).toEqual([
["(#1) Pivot"],
[""],
["Alice"],
["Olaf"],
["(Undefined)"],
]);
});

test("Order of undefined value is correct when ordered desc", () => {
// prettier-ignore
const grid = {
A1: "Customer", B1: "Price", C1: "=PIVOT(1)",
A2: "Alice", B2: "10",
A3: "", B3: "20",
A4: "Olaf", B4: "30",
};
const model = createModelFromGrid(grid);
addPivot(model, "A1:B4", {
rows: [{ fieldName: "Customer", order: "desc" }],
columns: [],
measures: [{ id: "price", fieldName: "Price", aggregator: "sum" }],
});
// prettier-ignore
expect(getEvaluatedGrid(model, "C1:C5")).toEqual([
["(#1) Pivot"],
[""],
["(Undefined)"],
["Olaf"],
["Alice"],
]);
});

test("Measure count as a correct label", () => {
const model = createModelWithPivot("A1:I5");
updatePivot(model, "1", {
Expand Down

0 comments on commit b2e40ea

Please sign in to comment.