我阅读了这篇文章并回答了 Songwut K. 在这个问题中给出的答案:
但我想知道可以在 _item 中使用第二个模型。假设 _item 是论坛中的一篇帖子,它从 $model 中检索数据,但我想使用一个不同的模型,比如 $comment 来对这篇帖子发表评论,并将帖子和评论一起作为一个 _item 查看。想象一下,该项目在 facebook 上发布,它仅显示撰写此帖子的文本、日期和用户。但是我如何从其他模型对此添加评论?我只想将我的 $comment 传递给 _item View 。
我尝试在我的 Controller 中添加新的 Commnet:
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
]);
}
}
并将其呈现给索引。但是我只能在索引中使用我的 $comment 如何将它传递给 _item?我试过这个:
<?php
echo ListView::widget( [
'dataProvider' => $dataProvider,
'itemView' => '_item',
'summary'=>'',
'comment' => $comment
]); ?>
并在我的 _item 中尝试:
<?= $model->getStatus($model->cnNewsContentType_Id); ?> <br>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($comment, 'Text')->textInput(['maxlength' => true])->label('Treść') ?>
<?php ActiveForm::end(); ?>
但有错误:
Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: yii\widgets\ListView::comment
在 _item 中我只能使用 $model。可以将我的 $comment 传递给 _item View 吗?请帮助我
最佳答案
你应该简单地使用 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.
例如:
<?= ListView::widget( [
'dataProvider' => $dataProvider,
'itemView' => '_item',
'viewParams' => ['comment' => $comment],
'summary'=>'',
]); ?>
关于php - Yii2 : How to pass additional parameters to listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35270623/