Yii2 - 将变量从视图传递到 Gridview 自定义操作列 [英] Yii2 - Pass variable from view to Gridview custom action columns
本文介绍了Yii2 - 将变量从视图传递到 Gridview 自定义操作列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想保存用户在单击页面 gridview 小部件中的编辑"按钮之前访问的最后一个地方.我创建了一个名为 $lastAddress 的变量,但我真的不知道如何将它传递到 gridview 并将其附加到编辑"按钮的 $url 变量中.谁能告诉我怎么做?
I want to save the last place that a user visited before he click onto "Edit" button in the gridview widget of a page. I created a variable named $lastAddress but I really dont know how to pass it onto the gridview and append it to the $url variable of "Edit" button. Can anyone show me how?
$lastAddress = 'xxx';
<?=
GridView::widget([
...
[
'class' => 'yiigridActionColumn',
'template' => '{view} {update} {delete}',
'buttons' => [
'update' => function ($url, $model) {
$url .= '&lastAddress=' . $lastAddress; //This is where I want to append the $lastAddress variable.
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url);
},
],
],
],
]);
?>
推荐答案
使用 使用
将变量从父作用域传递给闭包:
Use use
to pass in variables from the parent scope to a closure:
'update' => function ($url, $model) use ($lastAddress) {
$url .= '&lastAddress=' . $lastAddress; //This is where I want to append the $lastAddress variable.
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url);
},
这篇关于Yii2 - 将变量从视图传递到 Gridview 自定义操作列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文