Aug 20, 2013, 1:56 PM
[CLOSED] Sort store by multiple fields
Hello,
I was using the below override code to sort a store (locally) by multiple fields however it throws an error when I try with v2. How can I sort the store by multiple fields with the latest version?
Thanks.
I was using the below override code to sort a store (locally) by multiple fields however it throws an error when I try with v2. How can I sort the store by multiple fields with the latest version?
Thanks.
Ext.override(Ext.data.Store, {
/**
* Sort by multiple fields in the specified order.
* @param {Array} An Array of field sort specifications, or, if ascending
* sort is required on all columns, an Array of field names. A field specification
* looks like:<pre><code>
{
field: 'orderNumber',
direction: 'ASC'
}
*/
sortByFields: function (fields) {
// Collect sort type functions,
// Convert string field names to field+direction spec objects.
var st = [];
for (var i = 0; i < fields.length; i++) {
if (typeof fields[i] == 'string') {
fields[i] = {
field: fields[i],
direction: 'ASC'
};
}
st.push(this.fields.get(fields[i].field).sortType);
}
var fn = function (r1, r2) {
var result;
for (var i = 0; !result && i < fields.length; i++) {
var v1 = st[i](r1.data[fields[i].field]);
var v2 = st[i](r2.data[fields[i].field]);
result = (v1 > v2) ? 1 : ((v1 < v2) ? -1 : 0);
if (fields[i].direction == 'DESC') result = -result;
}
return result;
};
this.data.sort('ASC', fn);
if (this.snapshot && this.snapshot != this.data) {
this.snapshot.sort('ASC', fn);
}
this.fireEvent("datachanged", this);
}
});
Last edited by Daniil; Aug 21, 2013 at 9:07 AM.
Reason: [CLOSED]