[IMP] web: add auto sorting on measures in pivot and graph views

* Split documentation of graph and pivot views
 * Add an alphabetical auto sorting on measures in pivot and graph views

The pivot view should have it's own documentation since it is an
independant view.

We want to improve the user experience with graph and pivot views, the
measures were not sorted in any particular way hence they were displayed
in what looks like a random order. We chose to order them in an
alphabetical, making it more usable.
This commit is contained in:
RomainLibert
2018-07-10 12:39:44 +02:00
6 changed files with 92 additions and 10 deletions
@@ -88,7 +88,7 @@ var GraphController = AbstractController.extend(GroupByMenuInterfaceMixin,{
renderButtons: function ($node) {
if ($node) {
var context = {
measures: _.pairs(_.omit(this.measures, '__count__')),
measures: _.sortBy(_.pairs(_.omit(this.measures, '__count__')), function (x) { return x[0]; }),
};
this.$buttons = $(qweb.render('GraphView.buttons', context));
this.$measureList = this.$buttons.find('.o_graph_measures_list');
@@ -102,7 +102,7 @@ var PivotController = AbstractController.extend({
*/
renderButtons: function ($node) {
if ($node) {
var context = {measures: _.pairs(_.omit(this.measures, '__count'))};
var context = {measures: _.sortBy(_.pairs(_.omit(this.measures, '__count')), function(x) { return x[0]; })};
this.$buttons = $(QWeb.render('PivotView.buttons', context));
this.$buttons.click(this._onButtonClick.bind(this));
this.$buttons.find('button').tooltip();
@@ -119,5 +119,3 @@ var PivotView = AbstractView.extend({
return PivotView;
});
@@ -622,6 +622,30 @@ QUnit.module('Views', {
assert.ok(true,"should not generate any error");
graph.destroy();
});
QUnit.test('graph measures should be alphabetically sorted', function (assert) {
assert.expect(2);
var data = this.data;
data.foo.fields.bouh = {string: "bouh", type: "integer"};
var graph = createView({
View: GraphView,
model: "foo",
data: data,
arch: '<graph string="Partners">' +
'<field name="foo" type="measure"/>' +
'<field name="bouh" type="measure"/>' +
'</graph>',
})
assert.strictEqual(graph.$buttons.find('.o_graph_measures_list li:first').data('field'), 'bouh',
"Bouh should be the first measure");
assert.strictEqual(graph.$buttons.find('.o_graph_measures_list li:last').data('field'), '__count__',
"Count should be the last measure");
graph.destroy();
});
});
});
@@ -1178,4 +1178,28 @@ QUnit.module('Views', {
"should have 6 rows");
pivot.destroy();
});
QUnit.test('pivot measures should be alphabetically sorted', function (assert) {
assert.expect(2);
var data = this.data;
data.partner.fields.bouh = {string: "bouh", type: "integer"};
var pivot = createView({
View: PivotView,
model: "partner",
data: data,
arch: '<pivot>' +
'<field name="foo" type="measure"/>' +
'<field name="bouh" type="measure"/>' +
'</pivot>',
})
assert.strictEqual(pivot.$buttons.find('.o_pivot_measures_list li:first').data('field'), 'bouh',
"Bouh should be the first measure");
assert.strictEqual(pivot.$buttons.find('.o_pivot_measures_list li:last').data('field'), '__count',
"Count should be the last measure");
pivot.destroy();
});
});});
+42 -6
View File
@@ -784,7 +784,7 @@ The only allowed element within a graph view is ``field`` which can have the
following attributes:
``name`` (required)
the name of a field to use in a graph view. If used for grouping (rather
the name of a field to use in the view. If used for grouping (rather
than aggregating)
``type``
@@ -793,10 +793,9 @@ following attributes:
``row`` (default)
groups by the specified field. All graph types support at least one level
of grouping, some may support more. For pivot views, each group gets its
own row.
of grouping, some may support more.
``col``
only used by pivot tables, creates column-wise groups
authorized in graph views but only used by pivot tables
``measure``
field to aggregate within a group
@@ -805,13 +804,20 @@ following attributes:
``week``, ``month``, ``quarter`` or ``year``) instead of grouping on the
specific datetime (fixed second resolution) or date (fixed day resolution).
The measures are automatically generated from the model fields; only the
aggregatable fields are used. Those measures are also alphabetically
sorted on the string of the field.
.. warning::
graph view aggregations are performed on database content, non-stored
function fields can not be used in graph views
.. _reference/views/pivot:
Pivots
------
======
The pivot view is used to visualize aggregations as a `pivot table`_. Its root
element is ``<pivot>`` which can take the following attributes:
@@ -821,7 +827,37 @@ element is ``<pivot>`` which can take the following attributes:
``display_quantity``
Set to ``true`` to display the Quantity column by default.
The elements allowed within a pivot view are the same as for the graph view.
The only allowed element within a pivot view is ``field`` which can have the
following attributes:
``name`` (required)
the name of a field to use in the view. If used for grouping (rather
than aggregating)
``type``
indicates whether the field should be used as a grouping criteria or as an
aggregated value within a group. Possible values are:
``row`` (default)
groups by the specified field, each group gets its own row.
``col``
creates column-wise groups
``measure``
field to aggregate within a group
``interval``
on date and datetime fields, groups by the specified interval (``day``,
``week``, ``month``, ``quarter`` or ``year``) instead of grouping on the
specific datetime (fixed second resolution) or date (fixed day resolution).
The measures are automatically generated from the model fields; only the
aggregatable fields are used. Those measures are also alphabetically
sorted on the string of the field.
.. warning::
like the graph view, the pivot aggregates data on database content
which means that non-stored function fields can not be used in pivot views
.. _reference/views/kanban: