gpt4 book ai didi

gridview - 如何在 Yii2 gridview 中将操作按钮显示为下拉列表?

转载 作者:行者123 更新时间:2023-12-02 18:57:38 27 4
gpt4 key购买 nike

我想在 Yii 2 gridview 中将操作按钮显示为下拉菜单。我怎样才能在不使用任何扩展的情况下实现这一目标?

我已经添加了下面的源代码-

<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],

'id',
'name',

['class' => 'yii\grid\ActionColumn',
'template'=>'{view}{update}{delete}',
'buttons' => [
'view' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
'title' => Yii::t('app', 'View'),
]);
},
'update' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
'title' => Yii::t('app', 'Update'),
]);
},
],

'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'view') {
$url ='/site/view?id='.$model->id;
return $url;
}
if ($action === 'update') {
$url ='/site/update?id='.$model->id;
return $url;
}
}
],
],
]); ?>

最佳答案

这是我的做法:

use yii\bootstrap\ButtonDropdown;

// ... GridView configuration ...
[
'class' => 'yii\grid\ActionColumn',
'template' => '{all}',
'buttons' => [
'all' => function ($url, $model, $key) {
return ButtonDropdown::widget([
'encodeLabel' => false, // if you're going to use html on the button label
'label' => 'Options',
'dropdown' => [
'encodeLabels' => false, // if you're going to use html on the items' labels
'items' => [
[
'label' => \Yii::t('yii', 'View'),
'url' => ['view', 'id' => $key],
],
[
'label' => \Yii::t('yii', 'Update'),
'url' => ['update', 'id' => $key],
'visible' => true, // if you want to hide an item based on a condition, use this
],
[
'label' => \Yii::t('yii', 'Delete'),
'linkOptions' => [
'data' => [
'method' => 'post',
'confirm' => \Yii::t('yii', 'Are you sure you want to delete this item?'),
],
],
'url' => ['delete', 'id' => $key],
'visible' => true, // same as above
],
],
'options' => [
'class' => 'dropdown-menu-right', // right dropdown
],
],
'options' => [
'class' => 'btn-default', // btn-success, btn-info, et cetera
],
'split' => true, // if you want a split button
]);
},
],
],
// ... additional GridView configuration ...

您可以查看ButtonDropdown文档here .

关于gridview - 如何在 Yii2 gridview 中将操作按钮显示为下拉列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30286124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com