Yii2 output as gridview widget filter

I want to make private dropdownlist values ​​in the Gridview widgets of the YII2 structure. I now have the code:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [ //only fields name!
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'title',
        'statusId',
        'categoryId',
       ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

and statudId must be one of three possible values. (1-open, 2-stroke, 3-closed)

+6
source share
3 answers

Hi answer is simple from what you think.

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel'  => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'title',
            //don't use this:
            //'statusId',
            //use this instead:
            [
                'attribute' => 'statusId',
                'filter'    => [ "1"=>"open", "2"=>"in progress", "3"=>"closed" ]
            ],
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
+11
source

. . . ? , ("", " ", "") . " " .

0

Here's how you need to do this:

[
                'attribute'=>'column_name',
                'label'=>'LABEL OF COLUMN',
                'filter'=>array("first"=>"first","second"=>"second"), // you can read from database directly
                'value' => function ($data) {
                    return $data->column_name;
                }
            ],
0
source

Source: https://habr.com/ru/post/1649082/


All Articles