From 8a21e27377bb65c03ca9b4a67b228ce21519a3e9 Mon Sep 17 00:00:00 2001 From: RomainLibert Date: Tue, 10 Jul 2018 13:02:35 +0200 Subject: [PATCH] [IMP] web: add auto sorting on pivot view We add the attribute default_order on the pivot view (already present in graph view), this attribute will sort the pivot view on the given measure. Closes: #25675 --- .../static/src/js/views/pivot/pivot_model.js | 11 ++++++++++- .../static/src/js/views/pivot/pivot_view.js | 1 + addons/web/static/tests/views/pivot_tests.js | 18 ++++++++++++++++++ doc/reference/views.rst | 9 +++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) 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 b45f7a9ffdf..6001b25b3a5 100644 --- a/addons/web/static/src/js/views/pivot/pivot_model.js +++ b/addons/web/static/src/js/views/pivot/pivot_model.js @@ -238,9 +238,12 @@ var PivotModel = AbstractModel.extend({ * @param {string[]} params.colGroupBys * @param {string[]} params.measures * @param {Object} params.fields + * @param {string} params.default_order * @returns {Deferred} */ load: function (params) { + var self = this; + this.initialDomain = params.domain; this.initialRowGroupBys = params.context.pivot_row_groupby || params.rowGroupBys; this.fields = params.fields; @@ -254,7 +257,13 @@ var PivotModel = AbstractModel.extend({ sorted_column: {}, }; this.defaultGroupedBy = params.groupedBy; - return this._loadData(); + + return this._loadData().then(function () { + if (params.default_order) { + var info = params.default_order.split(' '); + self.sortRows(self.data.main_col.root.id, info[0], info[1] === 'desc'); + } + }); }, /** * @override diff --git a/addons/web/static/src/js/views/pivot/pivot_view.js b/addons/web/static/src/js/views/pivot/pivot_view.js index 643ee68a498..9ed3835a39a 100644 --- a/addons/web/static/src/js/views/pivot/pivot_view.js +++ b/addons/web/static/src/js/views/pivot/pivot_view.js @@ -95,6 +95,7 @@ var PivotView = AbstractView.extend({ this.loadParams.colGroupBys = colGroupBys; this.loadParams.rowGroupBys = rowGroupBys; this.loadParams.fields = this.fields; + this.loadParams.default_order = params.default_order || this.arch.attrs.default_order; this.controllerParams.title = params.title || this.arch.attrs.string || _t("Untitled"); this.controllerParams.enableLinking = !this.arch.attrs.disable_linking; diff --git a/addons/web/static/tests/views/pivot_tests.js b/addons/web/static/tests/views/pivot_tests.js index 70eee5b3431..138b620c4e0 100644 --- a/addons/web/static/tests/views/pivot_tests.js +++ b/addons/web/static/tests/views/pivot_tests.js @@ -1202,4 +1202,22 @@ QUnit.module('Views', { pivot.destroy(); }); + + QUnit.test('pivot view should use default order for auto sorting', function (assert) { + assert.expect(1); + + var pivot = createView({ + View: PivotView, + model: "partner", + data: this.data, + arch: '' + + '' + + '', + }); + + assert.ok(pivot.$('thead tr:last th:last').hasClass('o_pivot_measure_row_sorted_asc'), + "Last thead should be sorted in ascending order"); + + pivot.destroy(); + }); });}); diff --git a/doc/reference/views.rst b/doc/reference/views.rst index f91dbbaba21..524ce783be8 100644 --- a/doc/reference/views.rst +++ b/doc/reference/views.rst @@ -826,6 +826,15 @@ element is ```` which can take the following attributes: Set to ``True`` to remove table cell's links to list view. ``display_quantity`` Set to ``true`` to display the Quantity column by default. +``default_order`` + The name of the measure and the order (asc or desc) to use as default order + in the view. + + .. code-block:: xml + + + + The only allowed element within a pivot view is ``field`` which can have the following attributes: