[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
This commit is contained in:
Nicolas Lempereur
2019-02-26 14:54:46 +00:00
parent 463601a384
commit f8b38f64d3
2 changed files with 46 additions and 4 deletions
@@ -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
@@ -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);