本文介绍了如何在Yii2 gridview中将操作按钮显示为下拉菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Yii 2 gridview中将操作按钮显示为下拉列表.在不使用任何扩展的情况下如何实现?
I would like to display action button as dropdown in Yii 2 gridview. How can I achieve that without using any extension?
我已经添加了下面的源代码-
I have added the source code bellow-
<?= 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文档..
You can check ButtonDropdown documentation here.
这篇关于如何在Yii2 gridview中将操作按钮显示为下拉菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!