0

I want to give colors to rows of a GridView. But Yii2 show an error. It is like the model doesn't exist:

Showing the error "Trying to get property of non-object" This is part of my index view:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    function($model) {
        if ($model->quantity == 0) {
            return ['class' => 'danger'];
        }
    },
    'columns' => [
        'item',
        'quantity',
    ],
]) ?>

The data to fill the GridView is coming from a SQL query in the ProductsSearch model:

$query = Products::find()
    ->select(['item', 'quantity']);

$dataProvider = new ActiveDataProvider([ 
    'query' => $query->asArray(),
    'key' => 'item',
]);

return $dataProvider;

1 Answer 1

1

The error is here:

if ($model->quantity == 0) {
    return ['class' => 'danger'];
}

I changed it to:

if ($model['quantity'] == 0) {
    return ['class' => 'danger'];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.