如何在 Yii2 中将 2 个模型加载到单个视图中
How to load 2 models into a single view in Yii2
问题
此问题与 Yii2 项目有关,单击用户组名称时应显示相关的收件人列表。
目前我已经使用网格视图创建了所有数据库、模型、CRUD 生成并完美运行。
但问题是当我单击网格项时它会导航到另一个视图(默认方式) ) 如下图,我想在同一视图的一部分中加载它。
用户组(视图 -> index.php)
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//navigate to the relevent recipient list by click on the group name
'rowOptions' => function ($model, $key, $index, $grid) {
return [
'style' => "cursor: pointer",
'id' => $model['group_id'],
'onclick' => 'location.href="'.Yii::$app->urlManager->createUrl('recipient-list/recipients').'&scenario=RECIPIENTS¶ms="+(this.id);',
];
},
'columns' => [
'group_id',
'group_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
我在 GridView
中给出了 rowOptions
以便可以单击记录并通过 URL 导航到收件人列表。然后将从控制器中捕获并根据 group_id
过滤结果并呈现视图。
我想要的
我想在单击左侧组列表时动态加载收件人列表。
我试过了
iFrames
但无法正常工作。它在浏览器中作为一个单独的选项卡工作。
- 添加了许多 Yii 导航扩展,例如 kartik 的
sideNavs
用于组列表。但问题是它没有能力从数据库中获取数据。当我创建一个组时,它没有显示在列表中。 (如果侧边导航是静态的就好了)
So .. What I want to know is, is there are any way to do this with the controller or is there any Jquery code to support this...
热烈欢迎任何建议或参考。
当您点击行中的点击事件时
您将发出 ajax 请求以检索
第二个网格的数据(收件人列表)
在 ajax 请求成功或完成时
你会调用 $.pajax.reload 来刷新
第二个网格中的数据是这样的
$.pjax.reload({container:'#recipient-list'});
你可以使用 jQuery .load
来完成。
假设您的收件人列表容器 div 的 ID 为 #recipient-list
,因此您只需在 onclick
事件中添加以下内容:
$( "#recipient-list" ).load("YOUR_URL"); return false;
// replace YOUR_URL with the url you are generating already
// and don't forget the return false part. it't there so that link doesn't redirect
问题
此问题与 Yii2 项目有关,单击用户组名称时应显示相关的收件人列表。
目前我已经使用网格视图创建了所有数据库、模型、CRUD 生成并完美运行。
但问题是当我单击网格项时它会导航到另一个视图(默认方式) ) 如下图,我想在同一视图的一部分中加载它。
用户组(视图 -> index.php)
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//navigate to the relevent recipient list by click on the group name
'rowOptions' => function ($model, $key, $index, $grid) {
return [
'style' => "cursor: pointer",
'id' => $model['group_id'],
'onclick' => 'location.href="'.Yii::$app->urlManager->createUrl('recipient-list/recipients').'&scenario=RECIPIENTS¶ms="+(this.id);',
];
},
'columns' => [
'group_id',
'group_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
我在 GridView
中给出了 rowOptions
以便可以单击记录并通过 URL 导航到收件人列表。然后将从控制器中捕获并根据 group_id
过滤结果并呈现视图。
我想要的
我想在单击左侧组列表时动态加载收件人列表。
我试过了
iFrames
但无法正常工作。它在浏览器中作为一个单独的选项卡工作。- 添加了许多 Yii 导航扩展,例如 kartik 的
sideNavs
用于组列表。但问题是它没有能力从数据库中获取数据。当我创建一个组时,它没有显示在列表中。 (如果侧边导航是静态的就好了)
So .. What I want to know is, is there are any way to do this with the controller or is there any Jquery code to support this...
热烈欢迎任何建议或参考。
当您点击行中的点击事件时 您将发出 ajax 请求以检索 第二个网格的数据(收件人列表)
在 ajax 请求成功或完成时 你会调用 $.pajax.reload 来刷新 第二个网格中的数据是这样的
$.pjax.reload({container:'#recipient-list'});
你可以使用 jQuery .load
来完成。
假设您的收件人列表容器 div 的 ID 为 #recipient-list
,因此您只需在 onclick
事件中添加以下内容:
$( "#recipient-list" ).load("YOUR_URL"); return false;
// replace YOUR_URL with the url you are generating already
// and don't forget the return false part. it't there so that link doesn't redirect