From f8b38f64d3cad59228b9ab4bbc32507d915c6b7d Mon Sep 17 00:00:00 2001 From: Nicolas Lempereur Date: Mon, 25 Feb 2019 14:31:15 +0000 Subject: [PATCH] [FIX] web: keep col group by on 2nd reload In 187c32c2 some improvements were made for conserving/restoring group bys on the pivot view. This introduced an issue on the column group by: - before reload, if we set manually a column group by (+ icon in the header) it would be saved and not lost (unless a context or filter in the search view override it) - after reload, the following manually set column group by are not saved anymore on subsequent reload (the column group by at the first reload are set back) This behavior is not normal since the behavior (conserving or losing column group bys) should be the same at first reload or second reload. The issue was caused by the duplication of group bys that were not synchronized (one on the column root header, one on the pivot model instance). Without the change, the added test failed with: "Column groupby not lost after second reload" and product_id is hidding from the object pivot_column_groupby dictionary (having only "customer"). Using _.clone on initialRowGroupBys was necessary change because without duplication of "groupbys", the initialRowGroupBys would be changed on expandHeader which is unexpected. opw-1941095 closes #31407 --- .../static/src/js/views/pivot/pivot_model.js | 8 ++-- addons/web/static/tests/views/pivot_tests.js | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/addons/web/static/src/js/views/pivot/pivot_model.js b/addons/web/static/src/js/views/pivot/pivot_model.js index 90163c5ec272c..1e669824ec8a3 100644 --- a/addons/web/static/src/js/views/pivot/pivot_model.js +++ b/addons/web/static/src/js/views/pivot/pivot_model.js @@ -599,14 +599,14 @@ var PivotModel = AbstractModel.extend({ }); var index = 0; - var rowGroupBys = !_.isEmpty(this.data.groupedBy) ? this.data.groupedBy : this.initialRowGroupBys; + var rowGroupBys = !_.isEmpty(this.data.groupedBy) ? this.data.groupedBy : this.initialRowGroupBys.slice(); + this.data.groupedBy = rowGroupBys; var colGroupBys = this.data.colGroupBys; var datapt, row, col, attrs, cell_value; var main_row_header, main_col_header; var groupBys; var m; - for (var i = 0; i < rowGroupBys.length + 1; i++) { for (var j = 0; j < colGroupBys.length + 1; j++) { for (var k = 0; k < data[index].length; k++) { @@ -721,8 +721,8 @@ var PivotModel = AbstractModel.extend({ */ _updateMainGroupBys: function (old, main) { var new_groupby_length = this._getHeaderDepth(main.root) - 1; - var new_groupby_list = old.root.groupbys.slice(0, new_groupby_length); - main.root.groupbys = new_groupby_list; + main.root.groupbys = old.root.groupbys; + main.root.groupbys.splice(new_groupby_length); }, /** * @param {Object} old_tree diff --git a/addons/web/static/tests/views/pivot_tests.js b/addons/web/static/tests/views/pivot_tests.js index 2c22b5dcf50c7..1eb9fa94e30c1 100644 --- a/addons/web/static/tests/views/pivot_tests.js +++ b/addons/web/static/tests/views/pivot_tests.js @@ -988,6 +988,48 @@ QUnit.module('Views', { pivot.destroy(); }); + QUnit.test('Reload, group by columns, reload', function (assert) { + assert.expect(2); + + var pivot = createView({ + View: PivotView, + model: "partner", + data: this.data, + arch: '', + }); + + // Set a column groupby + pivot.$('thead .o_pivot_header_cell_closed').click(); + pivot.$('.o_field_selection li[data-field=customer] a').click(); + + // Set a domain + pivot.update({domain: [['product_id', '=', 41]]}); + + var expectedContext = {pivot_column_groupby: ['customer'], + pivot_measures: ['__count'], + pivot_row_groupby: []}; + + // Check that column groupbys were not lost + assert.deepEqual(pivot.getContext(), expectedContext, + 'Column groupby not lost after first reload'); + + // Set a column groupby + pivot.$('thead .o_pivot_header_cell_closed').click(); + pivot.$('.o_field_selection li[data-field=product_id] a').click(); + + // Set a domain + pivot.update({domain: [['product_id', '=', 37]]}); + + var expectedContext = {pivot_column_groupby: ['customer', 'product_id'], + pivot_measures: ['__count'], + pivot_row_groupby: []}; + + assert.deepEqual(pivot.getContext(), expectedContext, + 'Column groupby not lost after second reload'); + + pivot.destroy(); + }); + QUnit.test('correctly uses pivot_ keys from the context', function (assert) { assert.expect(7);