-
6 Nov 2012 1:11 PM #1
Answered: GroupingView - Custom Sort on GroupBy Column?
Answered: GroupingView - Custom Sort on GroupBy Column?
This strikes me as very simple, yet how to do this is eluding me.
Suppose we a table/grid with 3 columns:
- Group name (String)
- Field name (String)
- Field value (String)
I want to sort by 1) group then 2) name. Seems simple, right?
Creating the requisite ListStore, ColumnConfigs, Grid, and StoreSortInfo objects, I can achieve the sort ordering I want in the standard GridView.
When I change the Grid's view to a GroupingView, specifying the groupBy as (obviously) group name, my custom sorting now seems to be ignored. I can easily go back and forth comparing the grid views, and the behavior works as expected in a regular GridView, but the sorting on the groupBy field seems to be ignored by the GroupingView.
Any clues on what detail I'm missing to sort the groupBy column for a GroupingView?
-
Best Answer Posted by Cloudgatherer
I was referring to doing a local sort and I've figured out the detail. The createStoreSortInfo reference was the clue, thanks.
I had done the sorting via:
Instead, it must be done on the column config:Code:StoreSortInfo<Model> sortInfo = new StoreSortInfo<Model>(modelValueProvider, modelComparator, SortDir.ASC); store.addSortInfo(sortInfo);
GroupingView doesn't look at the first sort to see if that is correct, it automatically generates one by grabbing the Comparator from the ColumnConfig (which I had not set, and so I got the default sorting).Code:ColumnConfig<Model, String> modelColumnConfig = new ColumnConfig<Model, String>(modelValueProvider); modelColumnConfig.setComparator(modelComparator);
-
6 Nov 2012 3:28 PM #2
When you group by a field, the data set *must* be sorted by that field - that's how the similar values get positioned together. GroupingView.groupBy adds that column sort to the beginning of the list so that it will be prioritized over the other sorts.
It isn't clear if you are talking about local or remote sort, so I'll assume local.
The first sort then must be the groupBy column, which can be followed by your other two columns:
It doesn't otherwise affect the sort order.Code:if (grid.getLoader() == null || !grid.getLoader().isRemoteSort()) { lastStoreSort = createStoreSortInfo(column, SortDir.ASC); ds.addSortInfo(0, lastStoreSort);// this triggers the sort
GroupingView.doSort does however - if you cause it to be invoked (mostly by clicking a header or using the header menus), it will clear the existing sort, then if grouped, re-add that, and finally add the new sort.
Can you share how you are setting up your ListStore, StoreSortInfo, and Grid w/ GroupingView? If there is a bug, this will help us find it, and if there is something that doesn't make sense, the community might be able to help you spot it.
-
7 Nov 2012 10:19 AM #3
I was referring to doing a local sort and I've figured out the detail. The createStoreSortInfo reference was the clue, thanks.
I had done the sorting via:
Instead, it must be done on the column config:Code:StoreSortInfo<Model> sortInfo = new StoreSortInfo<Model>(modelValueProvider, modelComparator, SortDir.ASC); store.addSortInfo(sortInfo);
GroupingView doesn't look at the first sort to see if that is correct, it automatically generates one by grabbing the Comparator from the ColumnConfig (which I had not set, and so I got the default sorting).Code:ColumnConfig<Model, String> modelColumnConfig = new ColumnConfig<Model, String>(modelValueProvider); modelColumnConfig.setComparator(modelComparator);


Reply With Quote