From 8dbc1d95c36b18398c7284eddda2fe48842a8904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Mon, 9 Apr 2018 14:32:37 +0200 Subject: [IMP] web: many2one as measure in graph & pivot only when desirable With this commit, the fields many2one are not always measures in pivot and graph views. By default, they are *not* measures. In order to have them as measures, we should pass `additionalMeasures` as a param to any of the views, which contains a list of fields to consider as measures. example: Suppose a model with the following fields: - `product_id` (many2one) - `categ_id` (many2one) - `sold` (float) By default, only the field `sold` is a measure in a graph or pivot view. In order to pass `product_id` as a measure of a graph view, you should instantiate this view as follow: `` var viewInfo = /* ... */; var params = { additionalMeasures = ['product_id'], }; var graph = new GraphView(viewInfo, params); `` An exception to the rule above is when the many2one field is of type `measure` in the arch: this field is set as the default measure of the view, therefore it is implicitly added as an additional measure. --- addons/web/static/src/js/views/graph/graph_view.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'addons/web/static/src/js/views/graph/graph_view.js') diff --git a/addons/web/static/src/js/views/graph/graph_view.js b/addons/web/static/src/js/views/graph/graph_view.js index 8cfe2dc0272..54ff6642b15 100644 --- a/addons/web/static/src/js/views/graph/graph_view.js +++ b/addons/web/static/src/js/views/graph/graph_view.js @@ -36,12 +36,15 @@ var GraphView = AbstractView.extend({ /** * @override */ - init: function () { + init: function (viewInfo, params) { this._super.apply(this, arguments); + var self = this; var measure; var groupBys = []; + var measures = {__count__: {string: _t("Count"), type: "integer"}}; this.fields.__count__ = {string: _t("Count"), type: "integer"}; + this.arch.children.forEach(function (field) { var name = field.attrs.name; if (field.attrs.interval) { @@ -49,16 +52,17 @@ var GraphView = AbstractView.extend({ } if (field.attrs.type === 'measure') { measure = name; + measures[name] = self.fields[name]; } else { groupBys.push(name); } }); - var measures = {__count__: {string: _t("Count"), type: "integer"}}; _.each(this.fields, function (field, name) { if (name !== 'id' && field.store === true) { - if (field.type === 'integer' || field.type === 'float' || field.type === 'monetary' || field.type === 'many2one') { - measures[name] = field; + if (_.contains(['integer', 'float', 'monetary'], field.type) || + _.contains(params.additionalMeasures, name)) { + measures[name] = field; } } }); -- cgit v1.2.3