Reputation: 765
Is there a way to control the default sorting order used when first clicking a grid header? Suppose, I am having two columns one is name and another is downloads. i want to set name as ASC
order and downloads as DESC
on first click on grid header.that means when i first click on download column header it should be display most downloaded first.
Is it possible to set initial sorting order of column?
Upvotes: 4
Views: 4493
Reputation: 2013
I got a different solution
I had a similar situation, in which I wanted date columns to be sorted DESC on the first click, while others should be sorted ASC on the first click. I wrote my own GridView, and inside it I overridden the onHeaderClick function like so:
/**
* Make sure that Date columns are sorted in a DESCENDING order by default
*/
@Override
protected void onHeaderClick(Grid<ModelData> grid, int column)
{
if (cm.getColumn(column).getDateTimeFormat() != null)
{
SortInfo state = getSortState();
if (state.getSortField() != null && state.getSortField().equals(cm.getColumn(column).getId()))
{
super.onHeaderClick(grid, column);
return;
}
else
{
this.headerColumnIndex = column;
if (!headerDisabled && cm.isSortable(column))
{
doSort(column, SortDir.DESC);
}
}
}
else
{
super.onHeaderClick(grid, column);
return;
}
}
Upvotes: 2
Reputation: 765
I got solution.
You can Set initial Sorting direction using Store Sorter.
store.setStoreSorter(new StoreSorter<TemplateContentItem>(){
@Override
public int compare(Store<TemplateContentItem> store,
TemplateContentItem m1, TemplateContentItem m2,
String property) {
if(property.equals("downloads")){
return (super.compare(store, m1, m2, property) * -1);
}
return super.compare(store, m1, m2, property);
}
});
In above code, It will check if column is download than it will sort result in reverse.
Upvotes: -1