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' => 'yii\grid\ActionColumn',
'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屋!
查看全文