Yii gridview如何根据数据设置可编辑选项

时间:2016-09-19 05:24:27

标签: php gridview yii yii2

这是我的网格视图代码

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'toolbar'=> [
        ['content'=>
            Html::a('<i class="glyphicon glyphicon-plus"></i> Add New', ['create'],['class'=>'btn btn-shadow btn-success'])
        ],
        '{toggleData}',
    ],
    'columns' => [
        ['class' => 'kartik\grid\SerialColumn'],
        [
            'attribute'=>'Name',
            'vAlign'=>GridView::ALIGN_MIDDLE,
        ],
        [
            'attribute'=>'Age',
            'vAlign'=>GridView::ALIGN_MIDDLE,
            'value'=>'Age',
            'width'=>'5%',
            'hAlign'=>'center',
        ],
        [
            'attribute'=>'Address',
            'vAlign'=>GridView::ALIGN_MIDDLE,
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute'=>'Status',
            'vAlign'=>GridView::ALIGN_MIDDLE,
            'hAlign'=>GridView::ALIGN_CENTER,
            'width'=>'15%',
            'filterType'=>GridView::FILTER_SELECT2,
            'filterWidgetOptions'=>[
                'options' => ['placeholder' => 'Search Status...'],
                'pluginOptions' => [
                    'allowClear' => true
                ],
            ],
            'value'=>function($model){
                return ($model->Status=='0'?'Pending':($model->Status=='1'?'Accepted':'Rejected'));},
            'filter'=>['Pending','Diterima'],
            'label' => 'Status',
            'refreshGrid'=>true,
            'editableOptions'=>[
                'format' => Editable::FORMAT_BUTTON,
                'asPopover' => true,
                'inputType' => Editable::INPUT_DROPDOWN_LIST,
                'data'=>['0'=>'Pending', '1'=>'Accepted', '2'=>'Rejected'],
                'options' => ['class'=>'form-control'],
            ]
        ],
        [
            'class' => 'kartik\grid\ActionColumn',
            'template' => '
                <li>{view}</li>
                <li>{update}</li>
                <li>{delete}</li>',
            'dropdown'=>true,
            'dropdownOptions'=>['class'=>'pull-right'],
            'headerOptions'=>['class'=>'kartik-sheet-style'],
        ],
    ],
    'panel'=>[
        'type'=>GridView::TYPE_DEFAULT,
    ]
]); ?>

帮我查看Status属性。这个gridview的结果是这样的:
enter image description here

我想要的是这样的:
enter image description here

因此,就像Status is Pending一样,editableOption开启。但是当Status被接受或被拒绝时,editableOption被禁用。这可能吗?

1 个答案:

答案 0 :(得分:1)

您需要更改状态属性值选项

更改状态属性的值选项。

[
        'class' => 'kartik\grid\EditableColumn',
        'attribute'=>'Status',
          // ------- your code ------------
        'format'=>'raw',
        'value'=>function($model){
              if($model->Status=='0')
              {
                // if status =0 than create button and append with string
                $btn='<button type="button" id='.$model->id.' class="btn btn-default ><i class="fa fa-pencil-square-o"></i></button>';
                return 'Pending'.$btn;
              }
              else
              {
                return($model->Status=='1'?'Accepted':'Rejected');
              }
            },
        // ------- your code ------------

    ],