Как отобразить кнопку действия в виде раскрывающегося списка в представлении сетки Yii2?

Я хотел бы отобразить кнопку действия в виде раскрывающегося списка в виде сетки Yii 2. Как я могу добиться этого, не используя какое-либо расширение?

Я добавил исходный код ниже

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

person The Coder    schedule 17.05.2015    source источник


Ответы (2)


Вот как я это сделал:

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 здесь.

person Ledazinha    schedule 21.11.2016
comment
как я могу настроить URL-адрес в этом и показать представление, обновить действия с помощью всплывающего модального окна? - person noah_abou; 28.04.2017

Погуглите «Bootstrap Dropdowns», а затем замените действия в «template» => «{view}{update}{delete}» тем, что вы найдете. Оставив эти три действия вместо текста.

С уважением.

person JKGonePro    schedule 15.06.2016