serg
serg

Reputation: 111265

ExtJS6 - Change Grid Column Default Sort Direction from ASC to DESC

I have a grid linked to a remote store with a remote sorting enabled.

When I click on the grid's header for the first time it sets the column order to be ascending, then descending on the second click.

Is there a way to make the first click to go into the descending order for certain columns?

Upvotes: 2

Views: 3143

Answers (1)

JChap
JChap

Reputation: 1441

Hmm.. It took quite a bit of time to figure this out, seems like there is no direct API to alter the default direction.

This is what i figured finally, you need to update the defaultSortDirection of sorters in the store.

    store.getSorters().$sortable.setDefaultSortDirection('DESC');

Here is the Fiddle

Update

If you want to apply default sort direction at column level, then you could override the toggleSortState method at column level.

       columns: [{
            dataIndex: 'id',
            text: 'ID',
            width: 50,
            /**
             * Overriding this function to Change the Default Sort Order.
             */
            toggleSortState: function() {
                if (this.isSortable()) {
                    var me = this,
                        grid = me.up('tablepanel'),
                        store = grid.store,
                        sortParam = me.getSortParam(),
                        direction = undefined;

                    if(!store.getSorters().get(sortParam)) {
                        direction = 'DESC';
                    }                        
                    this.sort(direction);
                }
            },
        }

Here is the Fiddle

Upvotes: 2

Related Questions