Yii2 - 将视图中的变量传递给Gridview自定义操作列

时间:2015-07-06 08:44:34

标签: gridview yii2

我想保存用户在点击页面的gridview小部件中的“编辑”按钮之前访问的最后一个位置。我创建了一个名为$ lastAddress的变量,但我真的不知道如何将它传递给gridview并将其附加到“编辑”按钮的$ url变量。谁能告诉我怎么样?

$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);
                        },
                    ],
                ],
            ],
        ]);
        ?>

1 个答案:

答案 0 :(得分:52)

使用use将变量从父作用域传递到闭包:

'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);
},
相关问题