[FIX] web: pivot no result no remove groupbys

Since f8b38f64d dictionaries "groupbys" are the same object between the
pivot model instance and the headers menu.

But this causes an issue in 12.0 since when there is no results, the
headers menu groupbys is truncated and now being the same object, the
pivot model instance is also truncated.

So succedding search that find results would possibly have lost the
groupbys which is unexpected and breaks some 12.0 tests not expecting
it.

With this changeset, the groupbys are not truncated anymore.

Without the change, the added test fails with:

 Column groupby not lost after reload after empty results
  expected: { "pivot_column_groupby": [ "customer" ],
              "pivot_measures": [ "__count" ], "pivot_row_groupby": [] }
  result:   { "pivot_column_groupby": [], "pivot_measures":
              [ "__count" ], "pivot_row_groupby": [] }
closes #31644

Signed-off-by: Nicolas Lempereur (nle) <nle@odoo.com>
This commit is contained in:
Nicolas Lempereur
2019-03-06 17:02:35 +00:00
parent 7ef6557902
commit ee5f1e277d
2 changed files with 35 additions and 15 deletions
@@ -274,12 +274,10 @@ var PivotModel = AbstractModel.extend({
// if we don't have the key 'groupBy' in params. In that case,
// we want to have the full open state for the groupbys.
self._updateTree(old_row.root, self.data.main_row.root);
self._updateMainGroupBys(old_row, self.data.main_row);
}
if (!('pivot_column_groupby' in (params.context || {}))) {
self._updateTree(old_col.root, self.data.main_col.root);
self._updateMainGroupBys(old_col, self.data.main_col);
}
});
},
@@ -711,19 +709,6 @@ var PivotModel = AbstractModel.extend({
this._traverseTree(root.children[i], f, arg1, arg2, arg3);
}
},
/**
* Updates the groupBys that the main[row | col] holds
*
* @private
* @param {Object} old: The main abstract header before reload
* @param {Object} main: The main abstract header after reload
*
*/
_updateMainGroupBys: function (old, main) {
var new_groupby_length = this._getHeaderDepth(main.root) - 1;
main.root.groupbys = old.root.groupbys;
main.root.groupbys.splice(new_groupby_length);
},
/**
* @param {Object} old_tree
* @param {Object} new_tree
@@ -1030,6 +1030,41 @@ QUnit.module('Views', {
pivot.destroy();
});
QUnit.test('Empty results keep groupbys', 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 for empty results
pivot.update({domain: [['id', '=', false]]});
var expectedContext = {pivot_column_groupby: undefined,
pivot_measures: undefined,
pivot_row_groupby: undefined};
assert.deepEqual(pivot.getContext(), expectedContext,
'Column groupby not lost after empty results');
// Set a domain for not empty results
pivot.update({domain: [['product_id', '=', 37]]});
var expectedContext = {pivot_column_groupby: ['customer'],
pivot_measures: ['__count'],
pivot_row_groupby: []};
assert.deepEqual(pivot.getContext(), expectedContext,
'Column groupby not lost after reload after empty results');
pivot.destroy();
});
QUnit.test('correctly uses pivot_ keys from the context', function (assert) {
assert.expect(7);