簡體   English   中英

如何在一個視圖YII2中從另一個模型調用另一個DetailView :: widget?

[英]How to call another DetailView::widget from another model in one view YII2?

我是yii2 我在DetailView::widget有一個表的顯示數據。

我想為另一個表調用另一個DetailView::widget ,然后在同一視圖中顯示。

您可以在同一視圖中使用兩個DetailView小部件,這沒有問題。

在控制器中:

use yii\web\NotFoundHttpException;

...

public function actionView($id)
{
    $model1 = $this->findModel($id);
    $model2 = ModelName::find()->where(['id' => ...])->one();
    if (!$model2) {
        throw new NotFoundHttpException('Second model not found');
    }

    return $this->render('view', [
        'model1' => $model1,
        'model2' => $model2,
    ]);
}

鑒於:

<div class="col-md-6">
    <?= DetailView::widget([
        'model' => $model1,
        'attributes' => [
            'name',
        ],
    ]) ?>    
</div>

<div class="col-md-6">
    <?= DetailView::widget([
        'model' => $model2,
        'attributes' => [
            'name',
        ],
    ]) ?>
</div>

有關 DetailView小部件的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM