How to enable and disable sort in Yii2 GridView?

rdanusha picture rdanusha · Dec 13, 2014 · Viewed 42.1k times · Source

How to enable and disable sort in Yii2 GridView ?

Example

Answer

Ali MasudianPour picture Ali MasudianPour · Dec 13, 2014

You can customize columns sort in your DataProvider. For example if you use ActiveDataProvider in your GridView you can indicate sort-able columns like below:

$dataProvider = new ActiveDataProvider([
    'query' => Model::find(),
    'sort' => ['attributes' => ['column1','column2']]
]);

In above example, only column1 and column2 are sort-able.

You can also disable sorting for all columns like below:

'sort' =>false

It is suggested to take a look at Yii2's official document : Class yii\data\Sort As it defines it:

Sort represents information relevant to sorting. When data needs to be sorted according to one or several attributes, we can use Sort to represent the sorting information and generate appropriate hyperlinks that can lead to sort actions.