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
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'=>'',
]); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With