From 556ee204d0edc888f31193a8068503deef3d1ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Fri, 24 Mar 2017 16:51:34 +0100 Subject: [PATCH] [FIX] web: pivot view should respect the groupbys from the search view Before this commit, groupbys from the search view were totally ignored. --- .../static/src/js/views/pivot/pivot_model.js | 24 +++++++++++++---- addons/web/static/test/views/pivot_tests.js | 26 +++++++++++++++++++ 2 files changed, 45 insertions(+), 5 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 969a4d5ee82..ff0b80dd103 100644 --- a/addons/web/static/src/js/views/pivot/pivot_model.js +++ b/addons/web/static/src/js/views/pivot/pivot_model.js @@ -198,6 +198,10 @@ var PivotModel = AbstractModel.extend({ sortedColumn: this.data.sorted_column, }; }, + /** + * @param {string} id + * @returns {object} + */ getHeader: function (id) { return this.data.headers[id]; }, @@ -246,11 +250,20 @@ var PivotModel = AbstractModel.extend({ } else { this.data.domain = this.initialDomain; } + if ('groupBy' in params) { + this.data.groupedBy = params.groupBy; + } return this._loadData(params).then(function () { - self._updateTree(old_row_root, self.data.main_row.root); - var new_groupby_length = self._getHeaderDepth(self.data.main_row.root) - 1; - self.data.main_row.groupbys = old_row_root.groupbys.slice(0, new_groupby_length); + var new_groupby_length; + if (!('groupBy' in params)) { + // we only update the row groupbys according to the old groupbys + // 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); + new_groupby_length = self._getHeaderDepth(self.data.main_row.root) - 1; + self.data.main_row.groupbys = old_row_root.groupbys.slice(0, new_groupby_length); + } self._updateTree(old_col_root, self.data.main_col.root); new_groupby_length = self._getHeaderDepth(self.data.main_col.root) - 1; @@ -694,8 +707,9 @@ var PivotModel = AbstractModel.extend({ break; } } - if (tree) this._updateTree(tree, new_tree.children[i]); - else { + if (tree) { + this._updateTree(tree, new_tree.children[i]); + } else { new_tree.children[i].expanded = false; new_tree.children[i].children = []; } diff --git a/addons/web/static/test/views/pivot_tests.js b/addons/web/static/test/views/pivot_tests.js index efd33ccc139..bed3294cfe9 100644 --- a/addons/web/static/test/views/pivot_tests.js +++ b/addons/web/static/test/views/pivot_tests.js @@ -426,4 +426,30 @@ QUnit.module('Views', { pivot.destroy(); }); + QUnit.test('can be grouped with the update function', function (assert) { + assert.expect(4); + + var pivot = createView({ + View: PivotView, + model: "partner", + data: this.data, + arch: '' + + '' + + '', + }); + + assert.strictEqual(pivot.$('.o_pivot_cell_value').length, 1, + "should have only 1 cell"); + assert.strictEqual(pivot.$('tbody tr').length, 1, + "should have 1 rows"); + + pivot.update({groupBy: ['product_id']}); + + assert.strictEqual(pivot.$('.o_pivot_cell_value').length, 3, + "should have 3 cells"); + assert.strictEqual(pivot.$('tbody tr').length, 3, + "should have 3 rows"); + pivot.destroy(); + }); + });});