mathiasa
(Aeschlimann Mathias)
October 31, 2016, 10:04am
1
How can I add custom buttons to a GridView in Yii2? I’ve googled for this but strangely found nothing that I understood or that would work. I was able to do this in Yii1, but it works differently in Yii2.
If I have
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
],
]); ?>
What do I have to do to add to this a button that activates an action (and sends the id of the buttons row object)? I know there are already topics here on this issue, but I’m very confused, I must lack some basic understanding of the principles implemented in GridView.
EndErr
(Enderr)
November 1, 2016, 6:23am
2
mathiasa:
How can I add custom buttons to a GridView in Yii2? I’ve googled for this but strangely found nothing that I understood or that would work. I was able to do this in Yii1, but it works differently in Yii2.
If I have
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
],
]); ?>
What do I have to do to add to this a button that activates an action (and sends the id of the buttons row object)? I know there are already topics here on this issue, but I’m very confused, I must lack some basic understanding of the principles implemented in GridView.
First you schould create a action in your controller, then add that column in ‘columns’ => [ … ]
[
'format' => 'raw',
'value' => function($model, $key, $index, $column) {
return Html::a(
'<i class="fa fa-euro"></i>',
Url::to(['mycontroller/myaction', 'id' => $model->ID]),
[
'id'=>'grid-custom-button',
'data-pjax'=>true,
'action'=>Url::to(['mycontroller/myaction', 'id' => $model->ID]),
'class'=>'button btn btn-default',
]
);
}
],
Here I use the ‘url’ value of ‘Html::a’ and in ‘action’ custom attribute, this is done for my ajax code