Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add a button to grid view in yii2

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 ?

like image 894
sass Avatar asked Dec 03 '22 14:12

sass


1 Answers

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'
    ]   
]); ?>
like image 116
Barry Avatar answered Dec 24 '22 09:12

Barry