4

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 ?

sass
  • 73
  • 1
  • 2
  • 8

3 Answers3

18

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'
    ]   
]); ?>
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
Barry
  • 3,683
  • 1
  • 18
  • 25
0

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(); ?>
Bizley
  • 17,392
  • 5
  • 49
  • 59
0

Try this way:

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

More Info

Insane Skull
  • 9,220
  • 9
  • 44
  • 63
  • thanks !! i write this code now : `'countryCode', 'countryName', 'currencyCode', [ 'header' => 'Button', 'content' => function($model) { return Html::a($text='Test',$url='https://www.google.com'); } ], ` now i have antother problem , how can i make the text of this button one thing from database ? i mean how can i have for example a countryname column that actually be a button not just text ? @insaneSkull – sass Sep 27 '16 at 05:49
  • Use [contentOptions](http://www.yiiframework.com/doc-2.0/yii-grid-column.html#$contentOptions-detail) and [headerOptions](http://www.yiiframework.com/doc-2.0/yii-grid-column.html#$headerOptions-detail). add any html style you want. – Insane Skull Sep 27 '16 at 06:03
  • sorry but i don't understand ! i understand the $key is the ID but how can i acsses the text of my database ? – sass Sep 27 '16 at 06:19
  • @sass. You mean database value? it's in `$model` variable. – Insane Skull Sep 27 '16 at 06:21
  • yes database . i mean for example the button or link tag is in the line 6 then the text of the a tag be the one of the database values ! i write this code but it's not right :`'content' => function($model ,$key,$index, $column) { return Html::a($text=$model,$url='google.com'); }` – sass Sep 27 '16 at 06:25
  • @sass. really don't get it. show normal html anchor you want, i will convert in yii2 anchor. – Insane Skull Sep 27 '16 at 06:32
  • for example i have this column : `'countryCode'`, . i want have a column like this : `[ 'header' => 'edit City', 'content' => function($model,$key,$index, $column) { return Html::button('>',$options = ['onclick'=>'editshow('.$key.')','style' => ['border'=>'solid 2px blue']]); } ],` but i don't want the text be '>' . i want the text be the countryCode of that row . thanks :) ! – sass Sep 27 '16 at 06:55
  • @sass. Write `$model->countryCode` – Insane Skull Sep 27 '16 at 07:05
  • @sass. `Html::a($model->countryCode, $url, ['onclick'=>'editshow('.$key.')','style' => ['border'=>'solid 2px blue'])` – Insane Skull Sep 27 '16 at 07:06
  • @sass. Mark as helpful, so it can be useful for others. – Insane Skull Sep 27 '16 at 08:26
  • sorry for bothering you but i forgot to ask something . now i create my own cusom column but if i want give some option to a column that contain database column for example `'countryCode', 'countryName', 'currencyCode',` what shold i do ? i mean for example give this column a onclick attribute or ... . i actually want create something like KARTIK GRIDVIEW !!!! – sass Sep 27 '16 at 08:44
  • Use `['attribute' => 'column_name', 'value' => function($model) { return 'anything';}]` – Insane Skull Sep 27 '16 at 08:49