Mark116
Mark116

Reputation: 723

Custom Yii2 grid ActionButton

I've problem with a section of code that I've created to generate dinamically buttons that I need to include in my yii2 Grid ActionColumn. With this function I can define a button just specifying an array of parameter:

Function works pretty good, but I can't replace the static string "my name" with my variabile $config['icon'] because I can't send the value to the function.

Can I solve this problem? (I'm using Kartik grid estension)

foreach(...) {
  $actionColumns['controller'] = $config['controller'];
  $actionColumns['buttons']    = array($config['name'] => function ($url, $model, $key) {
                                                        return Html::a('my name', $url);
                                                    });
  $actionColumns['template']   = '{'.$config['name'].'}';
}

Ty

Upvotes: 0

Views: 86

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

I think you can passing the value by use for closure

 function ($url, $model, $key)   use  ($config['icon']) 
 {
   .....
 }

so in your case

foreach(...) {
   $actionColumns['controller'] = $config['controller'];
   $actionColumns['buttons']    = array($config['name'] => 
          function ($url, $model, $key) use  ($config['icon']) {
                  return Html::a($config['icon'], $url);
          });
   $actionColumns['template']   = '{'.$config['name'].'}';
}

Upvotes: 1

Related Questions