How to set the default sort direction when clicking a grid header column in GXT 2.2.5

Is there a way to control the default sort order used when the mesh header is first clicked? Suppose I have two columns, one name and one load. I want to set the name as ASC order and load as DESC when I first click on the grid header. This means that when I first click on the header of the load column, it should appear as the most loaded first.

Is it possible to set the initial sort order of a column?

+4
source share
2 answers

I got another solution

I had a similar situation in which I wanted the date columns to be sorted by DESC on the first click, while others should sort by ASC on the first click. I wrote my own GridView, and inside of it I redefined the onHeaderClick function as follows:

  /** * 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; } } 
+2
source

I got a solution.

You can set the initial direction of sorting using the Warehouse 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 the above code, it checks if the column is loaded, than it will sort the result in the reverse order.

-1
source

All Articles