如何在 GridView Yii2 中设置默认值
How to set Default value in GridView Yii2
我是 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",
],
],
]);
?>
但是我当时出错了:
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: 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 '-';
}
}
我是 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",
],
],
]);
?>
但是我当时出错了:
Unknown Property – yii\base\UnknownPropertyException Getting unknown property: 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 '-';
}
}