我是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",
],
],
]);
?>
但是我却出错了:
未知属性– yii \ base \ UnknownPropertyException获取未知属性:backend \ modules \ aitk \ models \ AitkRequest :: Rejected
有人请帮助我。如何设置带有条件值的默认值?
对于关注和帮助,谢谢。.:)
使用这样的闭包:
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;
}
尝试使用此代码:
'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 '-';
}
}