我想给GridView的行加颜色。但是Yii2显示错误。就像该模型不存在一样:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
function($model) {
if ($model->quantity == 0) {
return ['class' => 'danger'];
}
},
'columns' => [
'item',
'quantity',
],
]) ?>
填充GridView的数据来自 ProductsSearch 模型中的SQL查询:
$query = Products::find()
->select(['item', 'quantity']);
$dataProvider = new ActiveDataProvider([
'query' => $query->asArray(),
'key' => 'item',
]);
return $dataProvider;
答案 0 :(得分:1)
错误在这里:
if ($model->quantity == 0) {
return ['class' => 'danger'];
}
我将其更改为:
if ($model['quantity'] == 0) {
return ['class' => 'danger'];
}