Shop with custom sorting in Sencha Touch

I have a + store model that is connected to a third-party plugin (Ext.ux.TouchGridPanel). The plugin calls the store () sort method correctly with the appropriate mapping. Everything works fine, and the store sorts itself. However, I would prefer to add customer sorting to the store. I tried adding a sortType field to my model:

Ext.regModel("Transactions", { fields: [ { name: 'unit', type: 'string', sortType: function(value) { console.log('PRINT GDAMNIT'); return 0; } }, ... ] }); 

This, however, does not work, and sortType does not receive the call.

TL; DR: how to do regular sorting for stores?

+4
source share
4 answers

A sorter will be added to your store, which will sort in this field before it calls the sortType function.

 var store = new Ext.data.Store({ model: 'Transactions', sorters: [ { property: 'unit', direction: 'DESC' } ]} ); 

A sort type converts a field value to another value to ensure proper ordering. If you do not sort in this field, there is no reason to call this function. You can add sortDir to a field that sorts the field in ascending / descending order, depending on the type of field.

+6
source

The workaround may be that (I know this sounds inefficient, but carrying with me) add an extra field to the model instances (call sortField on it) and use this for your sort function. Then you can scroll through the model instances in your store, using your own sorting algorithm and assigning a sorting value of 0,1,2,3,4,5, etc. For sortField. Then in your store you can add 'sorters: 'sortField' ... I hope this helps a bit, I'm experiencing something similar at the moment.

+2
source

Sencha Touch 2's custom SortType works accordingly, according to http://docs.sencha.com/touch/2-0/#!/api/Ext.data.SortTypes :

 Ext.apply(Ext.data.SortTypes, { asPerson: function(person){ // expects an object with a first and last name property return person.lastName.toUpperCase() + person.firstName.toLowerCase(); } }); Ext.define('Employee', { extend: 'Ext.data.Model', config: { fields: [{ name: 'person', sortType: 'asPerson' }, { name: 'salary', type: 'float' // sortType set to asFloat }] } }); 
+1
source

What you are trying to do can be difficult. Calling store.sort() by default deletes all existing sorters (as per the Sencha Touch API documentation ). To save existing sorters, you need to add a sorter to the MixedCollection store.sorters .

Secondly, to call the sorting method using a special sorting function, you need to pass the specific sorterFn instead of property to Sorter (again, see the API ), but this can be difficult, as the sorting call is initiated from the plugin.

Not sure if this will help solve your problem, but maybe it will help you to look in the right direction.

0
source

Source: https://habr.com/ru/post/1348049/


All Articles