add a button to grid view in yii2

↘锁芯ラ 提交于 2020-01-03 20:12:52

问题


i'm a new yii2 developer ! i made a GridView and the code is shown below :

<?php Pjax::begin(); ?>    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\ActionColumn'],
            ['class' => 'yii\grid\CheckboxColumn'],
            ['class' => 'yii\grid\SerialColumn'],
            'id',
            'countryCode',
            'countryName',
            'currencyCode',
        ],
    ]); ?>
    <?php Pjax::end(); ?>

a screenshot of output : OUTPUT

now i want to have a column contain some button and that button for example open a page or somthing else ! my problem is how can i create that column ?


回答1:


You can also add the button (or as many as you like) to the existing action column like this

<?= GridView::widget([
    ::
    ::
    'columns' => [
        [
            'class' => 'yii\grid\ActionColumn',
            'template' => '{view} {update} {delete} {myButton}',  // the default buttons + your custom button
            'buttons' => [
                'myButton' => function($url, $model, $key) {     // render your custom button
                    return Html::a(..);
                }
            ]
        ]
        ::
        ::
        'currencyCode'
    ]   
]); ?>



回答2:


Example:

<?php Pjax::begin(); ?>
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\ActionColumn'],
        ['class' => 'yii\grid\CheckboxColumn'],
        ['class' => 'yii\grid\SerialColumn'],
        'id',
        'countryCode',
        'countryName',
        'currencyCode',
        [
            'label' => 'My Label',
            'format' => 'raw',
            'value' => Html::a('Click me', ['site/index'], ['class' => 'btn btn-success btn-xs', 'data-pjax' => 0])
        ]
    ],
]); ?>
<?php Pjax::end(); ?>



回答3:


Try this way:

[
            'header' => 'Button',
            'content' => function($model) {
                return Html::a(..);
            }           
],

More Info



来源:https://stackoverflow.com/questions/39716563/add-a-button-to-grid-view-in-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!