6

I want to make a closed dropdownlist values in the Gridview widget of YII2 framework. the code i have now:

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

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

and statudId should be one of 3 possible values. (1-open, 2-in progress, 3-closed)

2 Answers 2

15

Hi the 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'],
        ],
    ]); ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Value is not displaying as "1"=>"open","2"=>"in progress","3"=>"closed".
In Yii 1.1 We Use: 'value' => '$data->is_active?"Active":"Inactive"',
0

That's how you need to do it:

[
                '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;
                }
            ],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.