Hide column conditionally

I want to hide certain columns when the GridView is sorted by some other column than the default one. For this I need to know the following things:

  • Get the currently sorted column name (from dataprovider?)

  • Hide the column somehow (visible-property?)

How to achieve this? Thanks.

[size="2"]Since sorting is done via GET request you can just get the sorted param.[/size]

[size="2"]Get the current sorted option (if multiSort is not enabled):[/size]




$currentSort = Yii::$app->request->queryParams['sort'];



Yii\data\Sort Sort param

Make a grid column conditionally visible:




[

    'attribute' => 'name',

    'visible' => ($currentSort === '-name' ? true : false

],

yii\gird\Column visible

Thanks, got it! I’m using Pjax and it’s a bit question mark to me where the get-variables should be checked? In the beginning of index-file or somewhere else?