-
15 Apr 2012 10:14 PM #1
Answered: Sorting a store when record field with no value
Answered: Sorting a store when record field with no value
Hello,
I have been adding sorters on a store and noticed that if a record does not have a value for a field remains at the top of the sort.
Just wondering is there a way to order these records to the end of the list?
In this case if there is no consumerRating, I want the record to be at the bottom of the list.Code:--- The Store --- Ext.define('MyApp.store.PropertySummaries', { extend: 'Ext.data.Store', xtype: 'PropertySummaries', config: { model: 'MyApp.model.PropertySummary', sorters: ['propertyName', 'minPrice', 'maxAdults', 'consumerRating'] } }); .... --- called by --- theStore.sort('consumerRating', 'DESC');
Thanks in advance!
-
Best Answer Posted by mitchellsimoens
You could add your own Ext.util.Sorter with a custom sortFn to the store's sorters.
-
16 Apr 2012 5:31 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
- Answers
- 3113
You could add your own Ext.util.Sorter with a custom sortFn to the store's sorters.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
16 Apr 2012 3:33 PM #3
Thanks for the quick reply!
I tried with the below and that worked
I was wondering if there is a way to define multiple sorterFns?Code:theStore.sort([{ direction: 'ASC', sorterFn: function(o1, o2) { return compare(o1.data.consumerRating, o2.data.consumerRating); } }]); .... theStore.sort([{ direction: 'DESC', sorterFn: function(o1, o2) { return compare(o1.data.consumerRating, o2.data.consumerRating); } }]); .... // etc .... .... // not the most elegant function compare(value1, value2) { if (!value1 || value1 == '') { return -1; } if (!value2 || value2 == '') { return 1; } if (value1 > value2) { return 1; } if (value1 == value2) { return 0; } return -1; }
I tried to define multiple sorter functions but I think there is an error somewhere as the sorterFn isn't getting invoked (or default one is being invoked)
Thanks for your helpCode:Ext.define('StayzApp.store.PropertySummaries', { extend: 'Ext.data.Store', xtype: 'PropertySummaries', config: { model: 'StayzApp.model.PropertySummary', sorters: [ {property : 'propertyName'}, {property : 'minPrice'}, // TODO also define custom sorterFn {property : 'maxAdults'}, // TODO also define custom sorterFn { property : 'consumerRating', sorterFn: function(o1, o2) { return compare(o1.data.consumerRating, o2.data.consumerRating); } } ] } }); --- and to invoke --- theStore.sort('consumerRating', 'ASC'); or theStore.sort('consumerRating', 'DESC');


Reply With Quote