php - How to control template in gridview yii2 advanced template? -


i want give if condition control action buttons(view, edit , delete) in template attribute , add new buttons. have code, wrong:

[     'class' => 'yii\grid\actioncolumn',     'template'=>function ($session){         if($session->get('usertype') != 'admin'){             '{view}{update}{delete}';         }else{              'template' => '{view}{update}{delete}{activate}{deactivate}',             'buttons' => [                 'deactivate' => function ($url,\backend\models\document $model) {                     if($model->status==1)                         return html::a('<span class="glyphicon  glyphicon glyphicon-remove"></span>', $url, [                             'title' => yii::t('app', 'deactivate'),                         ]);                     },                 'activate' => function ($url, $model) {                     if($model->status==0)                         return html::a('<span class="glyphicon glyphicon glyphicon-ok"></span>', $url, [                             'title' => yii::t('app', 'activate'),                         ]);                     },                 ],             }         },     ], 

but got error :

object of class closure not converted string 

i using session object parameter in function. how fix that, or perfect way check if condition?

don't use anonymous function

 'template'=>function ( ... ) {} 

but call external function or directly use proper assigment code eg:

'template'=> (if($session->get('usertype') != 'admin')) ? '{view}{update}{delete}' : '{view}{update}{delete}{activate}{deactivate}',   public function mytemplate($session){    ....    return yourresult;  } 

then

'template'=> mytemplate($session), 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -