summaryrefslogtreecommitdiff
path: root/addons/base/static/src/js/data.js
diff options
context:
space:
mode:
authorXavier Morel <xmo@openerp.com>2011-05-04 16:05:35 +0200
committerXavier Morel <xmo@openerp.com>2011-05-04 16:05:35 +0200
commitff691a34b5b25ff2365ac8c0ba451757c50cfbf8 (patch)
tree0eadf3904ad995ac6b482a4645d7a6055e8dfd8b /addons/base/static/src/js/data.js
parente47ab260de4fe5f1fb0dfa0c724a91628ef60e6f (diff)
[ADD] sorting support in list views
* Fix sorting in DataSet.do_search_read (would not fix ordering of read results, yielding a semi-random order instead of the one returned by search) * Add sort parameter support to ListView.fill() * Improve handling of sort parameter on DataSetSearch - Queue up fields to sort on (most important last) by calling 'dataset.sort(fieldname)' repeatedly - Get back a sort string fit for sending to OpenERP (server) by calling 'dataset.sort()' (without arguments) * Adds sorting on list view by clicking on headers bzr revid: xmo@openerp.com-20110504140535-ydj8zs33lt0dgcwy
Diffstat (limited to 'addons/base/static/src/js/data.js')
-rw-r--r--addons/base/static/src/js/data.js33
1 files changed, 31 insertions, 2 deletions
diff --git a/addons/base/static/src/js/data.js b/addons/base/static/src/js/data.js
index 6d566f4de0a..c7c129e7fec 100644
--- a/addons/base/static/src/js/data.js
+++ b/addons/base/static/src/js/data.js
@@ -144,7 +144,7 @@ openerp.base.DataSetSearch = openerp.base.DataSet.extend({
init: function(session, model) {
this._super(session, model);
this.domain = [];
- this.sort = [];
+ this._sort = [];
this.offset = 0;
// subset records[offset:offset+limit]
// is it necessary ?
@@ -166,7 +166,7 @@ openerp.base.DataSetSearch = openerp.base.DataSet.extend({
fields: fields,
domain: this.domain,
context: this.context,
- sort: this.sort,
+ sort: this.sort(),
offset: offset,
limit: limit
}, function (records) {
@@ -177,6 +177,35 @@ openerp.base.DataSetSearch = openerp.base.DataSet.extend({
}
callback(records);
});
+ },
+ /**
+ * Reads or changes sort criteria on the dataset.
+ *
+ * If not provided with any argument, serializes the sort criteria to
+ * an SQL-like form usable by OpenERP's ORM.
+ *
+ * If given a field, will set that field as first sorting criteria or,
+ * if the field is already the first sorting criteria, will reverse it.
+ *
+ * @param {String} [field] field to sort on, reverses it (toggle from ASC to DESC) if already the main sort criteria
+ * @param {Boolean} [force_reverse=false] forces inserting the field as DESC
+ * @returns {String|undefined}
+ */
+ sort: function (field, force_reverse) {
+ if (!field) {
+ return _.map(this._sort, function (criteria) {
+ if (criteria[0] === '-') {
+ return criteria.slice(1) + ' DESC';
+ }
+ return criteria;
+ }).join(', ');
+ }
+
+ var reverse = force_reverse || (this._sort[0] === field);
+ this._sort = _.without(this._sort, field, '-' + field);
+
+ this._sort.unshift((reverse ? '-' : '') + field);
+ return undefined;
}
});