2016-07-02 67 views
0

我已经创建了一个代码段来生成dinamically按钮,我需要包括在我的yii2网格ActionColumn中的问题。有了这个功能,我可以定义一个按钮即可指定参数数组:自定义Yii2网格ActionButton

  • 控制器(目的地)
  • 图标

函数作品不错,但我不能代替静态字符串“我的名字”与我的variabile $ config ['icon'],因为我无法发送值的函数。

我可以解决这个问题吗? (我使用卡尔蒂克电网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'].'}'; 
} 

回答

1

我想你可以通过使用传递值关闭

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

所以你的情况

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'].'}'; 
}