Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 : How to pass additional parameters to listview

I read this post and answer give by Songwut K. in this question:

Yii2 ListView and dataprovider

But i want to know that is possible to use second model in _item. Suppose the _item is a post in the forum that retrieves data from the $model but I'd like to use a different model like $comment for me comment on a this post and view the post together with a commentary as one _item. Imagie that item is post on facebook and it display only text, date and user which write this post. But how i can add comment to this from other model? I just want to pass my $comment to _item view.

I was tried add new Commnet in my controller:

public function actionIndex()
    {
        $model = new NewsForm();
        $searchModel = new NewsSearch();
        $comment= new UrComment();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->setSort(['defaultOrder' => ['Id'=>SORT_DESC]]);

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->saveNews();
            return $this->redirect(['/content/news']);
        } else {
            return $this->render('index', [
                        'model' => $model,
                        'searchModel' => $searchModel,
                        'dataProvider' => $dataProvider,
                        'comment' => $comment
            ]);
        }
    }

And render this to index. But i can use my $comment only in index how can i pass this to _item? i tried this:

 <?php
                echo ListView::widget( [
                    'dataProvider' => $dataProvider,
                    'itemView' => '_item',
                    'summary'=>'', 
                    'comment' => $comment
                ]); ?>

And tried in my _item:

<?= $model->getStatus($model->cnNewsContentType_Id); ?> <br>
        <?php $form = ActiveForm::begin(); ?>
    <?= $form->field($comment, 'Text')->textInput(['maxlength' => true])->label('Treść') ?>
    <?php ActiveForm::end(); ?>

But have error:

Unknown Property – yii\base\UnknownPropertyException

Setting unknown property: yii\widgets\ListView::comment

In _item i can only use $model. It is possible to pass my $comment to _item view? Pls help me

like image 459
Informer Avatar asked Feb 08 '16 13:02

Informer


1 Answers

You should simply use viewParams :

Additional parameters to be passed to $itemView when it is being rendered. This property is used only when $itemView is a string representing a view name.

e.g. :

<?= ListView::widget( [
    'dataProvider' => $dataProvider,
    'itemView' => '_item',
    'viewParams' => ['comment' => $comment],
    'summary'=>'', 
]); ?>
like image 145
soju Avatar answered Sep 23 '22 08:09

soju