Answer the question
In order to leave comments, you need to log in
Yii2 How to display a list in a ListView but only with certain criteria?
There is a list
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'item'],
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('_list', ['model' => $model]);
},
]) ?>
Answer the question
In order to leave comments, you need to log in
The dataProvider contains an object of the ActiveQuery class, you write it there yourself, and that's where you need to specify the condition, for example:
...
'query' => Post::find(['user_id' => 1]),
...
try just filtering $model
like this:
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'item'],
'itemView' => function ($model, $key, $index, $widget) {
$newModel = array_filter($model, function($obj){
if($obj->post_author == 'vasia')
return true;
});
return $this->render('_list', ['model' => $newModel]);
},
]) ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question