[FIX] sorting list views

lp bug: https://launchpad.net/bugs/809309 fixed

bzr revid: xmo@openerp.com-20110713103725-y9fnpp36z78bjbhn
This commit is contained in:
Xavier Morel 2011-07-13 12:37:25 +02:00
parent 9e0738b97d
commit d1209c847b
3 changed files with 30 additions and 15 deletions

View File

@ -531,13 +531,13 @@ class DataSet(openerpweb.Controller):
class DataGroup(openerpweb.Controller): class DataGroup(openerpweb.Controller):
_cp_path = "/base/group" _cp_path = "/base/group"
@openerpweb.jsonrequest @openerpweb.jsonrequest
def read(self, request, model, fields, group_by_fields, domain=None): def read(self, request, model, fields, group_by_fields, domain=None, sort=None):
Model = request.session.model(model) Model = request.session.model(model)
context, domain = eval_context_and_domain(request.session, request.context, domain) context, domain = eval_context_and_domain(request.session, request.context, domain)
return Model.read_group( return Model.read_group(
domain or [], fields, group_by_fields, 0, False, domain or [], fields, group_by_fields, 0, False,
dict(context, group_by=group_by_fields)) dict(context, group_by=group_by_fields), sort or False)
class View(openerpweb.Controller): class View(openerpweb.Controller):
_cp_path = "/base/view" _cp_path = "/base/view"

View File

@ -1,6 +1,23 @@
openerp.base.data = function(openerp) { openerp.base.data = function(openerp) {
/**
* Serializes the sort criterion array of a dataset into a form which can be
* consumed by OpenERP's RPC APIs.
*
* @param {Array} criterion array of fields, from first to last criteria, prefixed with '-' for reverse sorting
* @returns {String} SQL-like sorting string (``ORDER BY``) clause
*/
openerp.base.serialize_sort = function (criterion) {
return _.map(criterion,
function (criteria) {
if (criteria[0] === '-') {
return criteria.slice(1) + ' DESC';
}
return criteria + ' ASC';
}).join(', ');
};
openerp.base.DataGroup = openerp.base.Controller.extend( /** @lends openerp.base.DataGroup# */{ openerp.base.DataGroup = openerp.base.Controller.extend( /** @lends openerp.base.DataGroup# */{
/** /**
* Management interface between views and grouped collections of OpenERP * Management interface between views and grouped collections of OpenERP
@ -130,7 +147,8 @@ openerp.base.ContainerDataGroup = openerp.base.DataGroup.extend(
context: this.context, context: this.context,
domain: this.domain, domain: this.domain,
fields: _.uniq(this.group_by.concat(fields)), fields: _.uniq(this.group_by.concat(fields)),
group_by_fields: this.group_by group_by_fields: this.group_by,
sort: openerp.base.serialize_sort(this.sort)
}, function () { }).then(function (response) { }, function () { }).then(function (response) {
var data_groups = _(response).map( var data_groups = _(response).map(
_.bind(self.transform_group, self)); _.bind(self.transform_group, self));
@ -174,7 +192,7 @@ openerp.base.ContainerDataGroup = openerp.base.DataGroup.extend(
self.session, self.model, group.__domain, self.session, self.model, group.__domain,
child_context, child_context.group_by, child_context, child_context.group_by,
self.level + 1), self.level + 1),
group); group, {sort: self.sort});
})); }));
}); });
} }
@ -197,8 +215,8 @@ openerp.base.GrouplessDataGroup = openerp.base.DataGroup.extend(
}, },
list: function (fields, ifGroups, ifRecords) { list: function (fields, ifGroups, ifRecords) {
ifRecords(_.extend( ifRecords(_.extend(
new openerp.base.DataSetSearch(this.session, this.model), new openerp.base.DataSetSearch(this.session, this.model),
{domain: this.domain, context: this.context})); {domain: this.domain, context: this.context, _sort: this.sort}));
} }
}); });
@ -444,16 +462,12 @@ openerp.base.DataSetSearch = openerp.base.DataSet.extend({
*/ */
sort: function (field, force_reverse) { sort: function (field, force_reverse) {
if (!field) { if (!field) {
return _.map(this._sort, function (criteria) { return openerp.base.serialize_sort(this._sort);
if (criteria[0] === '-') {
return criteria.slice(1) + ' DESC';
}
return criteria + ' ASC';
}).join(', ');
} }
var reverse = force_reverse || (this._sort[0] === field); var reverse = force_reverse || (this._sort[0] === field);
this._sort = _.without(this._sort, field, '-' + field); this._sort.splice.apply(
this._sort, [0, this._sort.length].concat(
_.without(this._sort, field, '-' + field)));
this._sort.unshift((reverse ? '-' : '') + field); this._sort.unshift((reverse ? '-' : '') + field);
return undefined; return undefined;

View File

@ -209,7 +209,7 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
self.dataset.sort($(this).data('id')); self.dataset.sort($(this).data('id'));
// TODO: should only reload content (and set the right column to a sorted display state) // TODO: should only reload content (and set the right column to a sorted display state)
self.reload_view(); self.reload_content();
}); });
this.$element.find('.oe-list-pager') this.$element.find('.oe-list-pager')
@ -449,6 +449,7 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
this.session, this.model, this.session, this.model,
results.domain, results.context, results.domain, results.context,
results.group_by); results.group_by);
this.groups.datagroup.sort = this.dataset._sort;
if (_.isEmpty(results.group_by) && !results.context['group_by_no_leaf']) { if (_.isEmpty(results.group_by) && !results.context['group_by_no_leaf']) {
results.group_by = null; results.group_by = null;