4

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dipesh Gandhi
  • 765
  • 1
  • 13
  • 31

2 Answers2

2

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;
        }
    }
Tom Teman
  • 1,975
  • 3
  • 28
  • 43
-1

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.

Dipesh Gandhi
  • 765
  • 1
  • 13
  • 31
  • 1
    Header 'sorting order arrow' will be in wrong position in your case. Moreover I think it is bad idea to move that logic into sorter. – 4ndrew Mar 14 '14 at 08:19
  • 2
    Thanks for correcting me. You can use Tom Teman's solution, it's a good solution and i think it will solve your sorting order arrow issue also. – Dipesh Gandhi Mar 14 '14 at 19:42