From 5987e95ec2f4ee288ee9c07dc64d6d3ed99027b3 Mon Sep 17 00:00:00 2001 From: FrancoisGe Date: Tue, 20 Jun 2023 07:51:52 +0000 Subject: [PATCH] [FIX] web: sort x2m on multi fields Before this commit, go to a form view with an x2m displayed in list mode. If you sort the x2m on two differents fields, a crash is displayed. How to reproduce: - Go to a form view with an x2m in list mode with at least 2 columns sortable. - Click on a column to order - Click on a second column to order Before this commit: We have a crash After this commit: The list is ordered correctly Part-of: odoo/odoo#114024 --- .../src/model/relational_model/static_list.js | 2 +- .../views/fields/one2many_field_tests.js | 41 ++++++++++++------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/addons/web/static/src/model/relational_model/static_list.js b/addons/web/static/src/model/relational_model/static_list.js index 63a01dc7dd5..62616d0f7ff 100644 --- a/addons/web/static/src/model/relational_model/static_list.js +++ b/addons/web/static/src/model/relational_model/static_list.js @@ -27,7 +27,7 @@ function compareRecords(r1, r2, orderBy, fields) { return 1; } if (orderBy.length > 1) { - return compareRecords(r1, r2, orderBy.slice(1)); + return compareRecords(r1, r2, orderBy.slice(1), fields); } return 0; } diff --git a/addons/web/static/tests/views/fields/one2many_field_tests.js b/addons/web/static/tests/views/fields/one2many_field_tests.js index bee3d16f3d2..a64b413c2fc 100644 --- a/addons/web/static/tests/views/fields/one2many_field_tests.js +++ b/addons/web/static/tests/views/fields/one2many_field_tests.js @@ -2885,9 +2885,9 @@ QUnit.module("Fields", (hooks) => { QUnit.test("sorting one2many fields", async function (assert) { serverData.models.partner.fields.foo.sortable = true; - serverData.models.partner.records.push({ id: 23, foo: "abc" }); - serverData.models.partner.records.push({ id: 24, foo: "xyz" }); - serverData.models.partner.records.push({ id: 25, foo: "def" }); + serverData.models.partner.records.push({ id: 23, foo: "abc", int_field: 1 }); + serverData.models.partner.records.push({ id: 24, foo: "xyz", int_field: 1 }); + serverData.models.partner.records.push({ id: 25, foo: "def", int_field: 2 }); serverData.models.partner.records[0].p = [23, 24, 25]; let rpcCount = 0; @@ -2900,6 +2900,7 @@ QUnit.module("Fields", (hooks) => { + `, @@ -2910,22 +2911,34 @@ QUnit.module("Fields", (hooks) => { }); rpcCount = 0; - assert.strictEqual( - [...target.querySelectorAll(".o_data_cell")].map((c) => c.textContent).join(" "), - "abc xyz def" + assert.deepEqual( + [...target.querySelectorAll(".o_data_cell[name='foo']")].map((c) => c.textContent), + ["abc", "xyz", "def"] ); - await click(target.querySelector("table thead .o_column_sortable")); + await click(target.querySelector("table thead [data-name='foo'].o_column_sortable")); assert.strictEqual(rpcCount, 0, "in memory sort, no RPC should have been done"); - assert.strictEqual( - [...target.querySelectorAll(".o_data_cell")].map((c) => c.textContent).join(" "), - "abc def xyz" + assert.deepEqual( + [...target.querySelectorAll(".o_data_cell[name='foo']")].map((c) => c.textContent), + ["abc", "def", "xyz"] ); - await click(target.querySelector("table thead .o_column_sortable")); - assert.strictEqual( - [...target.querySelectorAll(".o_data_cell")].map((c) => c.textContent).join(" "), - "xyz def abc" + await click(target.querySelector("table thead [data-name='foo'].o_column_sortable")); + assert.deepEqual( + [...target.querySelectorAll(".o_data_cell[name='foo']")].map((c) => c.textContent), + ["xyz", "def", "abc"] + ); + + await click(target.querySelector("table thead [data-name='int_field'].o_column_sortable")); + assert.deepEqual( + [...target.querySelectorAll(".o_data_cell[name='foo']")].map((c) => c.textContent), + ["xyz", "abc", "def"] + ); + + await click(target.querySelector("table thead [data-name='int_field'].o_column_sortable")); + assert.deepEqual( + [...target.querySelectorAll(".o_data_cell[name='foo']")].map((c) => c.textContent), + ["def", "xyz", "abc"] ); });