gridview小部件过滤器中的yii2下拉列表

时间:2016-07-25 21:49:12

标签: php yii2

我想在YII2框架的Gridview小部件中创建一个关闭的下拉列表值。我现在的代码:

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

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

和statudId应该是3个可能的值之一。 (1开,2进,3关)

2 个答案:

答案 0 :(得分:11)

您的回答很简单。

<?= 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'],
        ],
    ]); ?>

答案 1 :(得分:0)

这就是您需要这样做的方式:

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