问题
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