如何在GridView Yii2中设置默认值

问题描述 投票:1回答:2

我是Yii2的新手,我需要有关GridView Yii2的一些帮助GridView Yii2

我试图使条件像这里一样:

   <?=
GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
         /*........Other attribute here..........*/
         [
            'attribute' => 'status_dosen',
            'value' => "status_dosen"==1 ? "Approved": "status_dosen"==NULL ? "Pending": "Rejected",
        ],
        [
            'attribute' => 'status_asrama',
            'value' => "status_dosen"==1 ? "Approved": "status_dosen"==NULL ? "Pending": "Rejected",
        ],

        ],
]);

?>

但是我却出错了:

Error

未知属性– yii \ base \ UnknownPropertyException获取未知属性:backend \ modules \ aitk \ models \ AitkRequest :: Rejected

有人请帮助我。如何设置带有条件值的默认值?

对于关注和帮助,谢谢。.:)

gridview yii2 yii2-advanced-app
2个回答
1
投票

使用这样的闭包:

GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
             /*........Other attribute here..........*/
             [
                'attribute' => 'status_dosen',
                'value' => function ($data){
 return $data->status_dosen==1 ? "Approved": ($data->status_dosen==NULL ? "Pending": "Rejected");
}
            ],

    ]);

请参见more

编辑:

使用CSS。添加HTML或CSS文件:

   .table-striped>tbody>tr:nth-child(odd)>td, .table-striped>tbody>tr:nth-child(odd)>th {
      background-color: red;
    }

.table-striped>tbody>tr:nth-child(even)>td, .table-striped>tbody>tr:nth-child(even)>th {
      background-color: green;
    }

1
投票

尝试使用此代码:

'value' => function ($model) {
    return getStatusHtml($model->status);
}

某些功能:

function getStatusHtml($status)
    {
        $text = '';
        if ($status == 1) {
            return '<span class="label label-success>Success</span>';
        } elseif ($status == 2) {
            return '<span class="label label-default>Default</span>';
        } else {
            return '-';
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.