oria / gridx

Just another powerful Dojo grid
Other
162 stars 78 forks source link

Sort by attribute other than 'field' ? #34

Open jcaselman opened 11 years ago

jcaselman commented 11 years ago

I have an object which has a human readable value as a string which would require a complex function to sort using a comparator in the Grids structure, and from reading Issue #23 it seem to indicate that that only works for the old ItemFileWriteStore which I am not using. An integer value already exists in this object to facilitate sorting.

I was able to achieve this by setting the 'id' in the structure to the value I want used for the sort attribute, the 'field' to the value I want displayed, and commenting out the portion of the private ._sort() method in 'gridx/core/model/extensions/Sort.js' that tries to use the column's field for the sort attribute. (GridX 1.1, .zip downloaded today)

55| if(s.colId){
56|     s.attribute = /*c.columns ? (c.columns[s.colId].field || s.colId) :*/ s.colId;
57| }else{

I haven't noticed any problems (yet, all my Grid id's are the same as their fields, except for this one), but I have to ask, is there already, or is there a planned implementation of a way to do this? Something like adding a sortBy: 'foo' to the column object in the structure?

zhuxw commented 11 years ago

Hi, gridx/core/model/extensions/FormatSort already supports the new store now. You can use it to sort formatted columns. The column id is to uniquely identify a column, while the "field" is to map the column to a data field of store. The "field" is optional if "formatter" is provided. But sorting needs this "field" because it is actually done in store, and store knows nothing about grid. But if you use FormatSort extension, you can provide a "comparator" function in column definition to sort a formatted column. Please refer to gridx/tests/test_grid_sort for a demo.