yii - Yii2 GridView hide column conditionally -
i displaying columns in yii2 gridview widget, 'executive name' 1 of should displayed when supervisor logged in not when executive logged in.
when hard coding visible 0 not displaying follows:
[ 'label' => 'executive name', 'attribute' => 'cs.first_name', 'visible' => '0', ],
but want display conditionally this:
[ 'label' => 'executive name', 'attribute' => 'cs.first_name', 'visible' => function ($data) { if ($data->hc_customersupport->is_supervisor) { return '1'; // or return true; } else { return '0'; // or return false; } }, ],
please tell if approach correct.
yii\grid\datacolumn
extended yii\grid\column
has visible property. can see docs, accepts boolean values, of course can dynamically calculate passing expression returning boolean value. example rbac:
use yii; ... 'visible' => yii::$app->user->can('supervisor'),
passing callable not allowed , doesn't make sense. logically think - why visibility of whole column dependent concrete row (model)?
p.s. should return boolean, not integer or string. expression can reduced this:
return $data->hc_customersupport->is_supervisor;
but is_supervisor
check definetely wrong, should not called (through model). it's better use rbac instead.
Comments
Post a Comment