S
S
Stepan2015-02-04 19:22:11
Yii
Stepan, 2015-02-04 19:22:11

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]);
        },
    ]) ?>

So it is necessary that the model does not fall all but only with, for example, post_author = vasia

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Semenko, 2015-02-04
@abler98

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]),
...

A
Andrew Vakulenko, 2015-02-04
@pro100ShCoder

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 question

Ask a Question

731 491 924 answers to any question