Skip to content

Commit

Permalink
[FIX] web: keep col group by on 2nd reload
Browse files Browse the repository at this point in the history
In 187c32c 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 odoo#31407
  • Loading branch information
nle-odoo committed Feb 26, 2019
1 parent 463601a commit f8b38f6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
8 changes: 4 additions & 4 deletions addons/web/static/src/js/views/pivot/pivot_model.js
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions addons/web/static/tests/views/pivot_tests.js
Expand Up @@ -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: '<pivot/>',
});

// 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);

Expand Down

0 comments on commit f8b38f6

Please sign in to comment.