[IMP] improves sorting mechanisms in datasets
Create a deserialize_sort method and add a set_sort method to datasets. The set_sort method is useful to avoid duplicating code in lists and kanban views (to set the initial default order)
This commit is contained in:
parent
abf4ca3120
commit
5f09c7dedc
|
@ -21,6 +21,17 @@ instance.web.serialize_sort = function (criterion) {
|
|||
}).join(', ');
|
||||
};
|
||||
|
||||
/**
|
||||
* Reverse of the serialize_sort function: convert an array of SQL-like sort
|
||||
* descriptors into a list of fields prefixed with '-' if necessary.
|
||||
*/
|
||||
instance.web.deserialize_sort = function (criterion) {
|
||||
return _.map(criterion, function (criteria) {
|
||||
var split = _.without(criteria.split(' '), '');
|
||||
return (split[1] && split[1].toLowerCase() === 'desc' ? '-' : '') + split[0];
|
||||
});
|
||||
};
|
||||
|
||||
instance.web.Query = instance.web.Class.extend({
|
||||
init: function (model, fields) {
|
||||
this._model = model;
|
||||
|
@ -685,6 +696,15 @@ instance.web.DataSet = instance.web.Class.extend(instance.web.PropertiesMixin,
|
|||
this._sort.unshift((reverse ? '-' : '') + field);
|
||||
return undefined;
|
||||
},
|
||||
/**
|
||||
* Set the sort criteria on the dataset.
|
||||
*
|
||||
* @param {Array} fields_list: list of fields order descriptors, as used by
|
||||
* Odoo's ORM (such as 'name desc', 'product_id', 'order_date asc')
|
||||
*/
|
||||
set_sort: function (fields_list) {
|
||||
this._sort = instance.web.deserialize_sort(fields_list);
|
||||
},
|
||||
size: function () {
|
||||
return this.ids.length;
|
||||
},
|
||||
|
|
|
@ -357,10 +357,7 @@ instance.web.ListView = instance.web.View.extend( /** @lends instance.web.ListVi
|
|||
var default_order = this.fields_view.arch.attrs.default_order,
|
||||
unsorted = !this.dataset._sort.length;
|
||||
if (unsorted && default_order) {
|
||||
_.each(default_order.split(',').reverse(), function (order) {
|
||||
var split = _.without(order.split(' '), '');
|
||||
self.dataset.sort(split[0], split[1] === 'desc');
|
||||
});
|
||||
this.dataset.set_sort(default_order.split(','));
|
||||
}
|
||||
|
||||
if(this.dataset._sort.length){
|
||||
|
|
|
@ -61,17 +61,13 @@ instance.web_kanban.KanbanView = instance.web.View.extend({
|
|||
$('html').off('click.kanban');
|
||||
},
|
||||
load_kanban: function(data) {
|
||||
var self = this;
|
||||
this.fields_view = data;
|
||||
|
||||
// use default order if defined in xml description
|
||||
var default_order = this.fields_view.arch.attrs.default_order,
|
||||
unsorted = !this.dataset._sort.length;
|
||||
if (unsorted && default_order) {
|
||||
_.each(default_order.split(',').reverse(), function (order) {
|
||||
var split = _.without(order.split(' '), '');
|
||||
self.dataset.sort(split[0], split[1] === 'desc');
|
||||
});
|
||||
this.dataset.set_sort(default_order.split(','));
|
||||
}
|
||||
|
||||
this.$el.addClass(this.fields_view.arch.attrs['class']);
|
||||
|
|
Loading…
Reference in New Issue