Search code examples
gridviewyii2yii2-advanced-appyii2-model

How to change sorter param in Yii2 / GridView?I


I have two tables with farily same names of the columns, usinng two different search models (both using ActiveDataProvider's) so when I sort one by a column, the other gets affected as well.

I have tried setting this in my second GridView:

'sorter' => [
    'class' => 'yii\widgets\LinkSorter',
    'sortParam' => 'sortB',
],

But with no avail.


EDIT: Sort param is a GET variable passed to server: ?sort=amount, or ?param=1&sort=created_at.


Solution

  • $sortParam is a property of yii\data\Sort, which handles the sorting on the DataProvider level and is accessable via $dataProvider->sort. The $sorter on the GridView is only a widget that can display the sorting links, but that is inherited from BaseListView and not displayed in GridView by default.

    You should set this property on the DataProvider like this:

    $dataProvider->sort->sortParam = 'user-sort';
    

    The same applies to the pageParam too. There is a section in the guide about Multiple GridViews on one page covering exactly this topic.