Yii2 GridView列属性值

时间:2018-02-09 07:00:19

标签: gridview yii2

我有两种类型的选择(radioList& dropDownList),如下所示,以及如何查看STRING的值而不是INT的值?我应该填写什么价值?

我的结果是Undefined variable:model

谢谢你

 Vitamin | Type

 (A | Tablet) 

 (**NOT** 0 | 2)

my _form.php

 <?= $form->field($model, 'vitamin')
 ->radioList(array(0=>'A', 1=>'B', 2=>'C'))
 ->label('Choose Vitamin') 
 ?>


 <?= $form->field($model, 'type')
 ->dropDownList(
 ['1' => 'liquid', '2' => 'Tablet', '3' => 'Pills', ['prompt'=>'Choose Package']
 ); ?>

我的index.php

 GridView::widget([
 'dataProvider' => $dataProvider,
 'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'attribute' => 'vitamin', 
            'filter'=>array("0"=>"A","1"=>"B","2"=>"C"),
            'value' => $model->vitamin == 0 ? 'A' : $model->vitamin == 1 ? 'B' : 'C'
       ],
       [
            'attribute' => 'type',
            'filter'=>array("1"=>"Liquid","2"=>"Tablet","3"=>"Pills"),
            'value' => $model->type == 1 ? 'Liquid' : $model->type == 2 ? 'Vitamin' : 'Pills'
       ],

1 个答案:

答案 0 :(得分:0)

我找到了答案

    [
        'attribute' => 'vitamin', 
        'filter'=>array("0"=>"A","1"=>"B","2"=>"C"),
        'value' => function ($model) {
          return $model->vitamin == 0 ? 'A' : $model->vitamin == 1 ? 'B' : 'C';
        }
   ],
   [
        'attribute' => 'type',
        'filter'=>array("1"=>"Liquid","2"=>"Tablet","3"=>"Pills"),
        'value' => function ($model) {
         return $model->type == 1 ? 'Liquid' : $model->type == 2 ? 'Vitamin' : 'Pills'
         }
   ],