Search code examples
phpsortinggridviewyii2dataprovider

Yii2 GridView widget, need to reverse on-click sort order


I need help with Yii2 GridView widget. Issue is: If you click on unsorted column name it will sort, by default from min to max(like SORT_ASC const in defaultOrder). What should i change to reverse sort order on click, it should be from max to min(like SORT_DESC).

There is no problem to choose default sort order onload, i need change it's onclick order. Sort options of dataProvider are:

            'sort' => [
            'attributes' => ['weekly_length','name', 'market','unique','sessions','retentions', 'session_length_summary',],
            'defaultOrder' => ['weekly_length' => SORT_DESC],
          ],

Solution

  • Just Reverse Your Sort Order as : -

    'sort' => [
                'attributes' => [
                    'weekly_length' => [
                        'asc' => ['weekly_length' => SORT_DESC,],
                        'desc' => ['weekly_length' => SORT_ASC],
                    ]
                ]
            ]