Today we are going to learn how to create dropdown in yii2 gridview. In this post, I will show you how we can add dropdown list in grid view filter and in the column value.
Let’s start.
1 2 3 4 5 6 7 8 9 10 11 12 |
[ 'attribute' => 'status', 'filter' => ['A'=>'Active','I'=>'Inactive','B'=>'Block'], 'filterInputOptions' => [ 'class' => 'form-control', 'prompt' => 'Select' ], 'format' => 'raw', 'value' => function ($model) { return Html::dropDownList('status', $model->status, ['A'=>'Active','I'=>'Inactive','B'=>'Block'], ['onchange' => "changeStatus($(this).val(),'$model->id')"]); }] |
In the above example, I create a dropdown for the status column. The resultant images are below:
(dropdown in yii2 gridview)
image for filter
(dropdown in gridiew filter)
you can update status with the help of changeStatus() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<script> function changeStatus(val, id) { // console.log(val, id); $.ajax({ url: "<?= Url::to(['update-status']) ?>", data: {'val': val, id: id}, cache: false }).done(function (html) { location.reload(); }); } </script> |
Hope this will help you 🙂