简体   繁体   English

如何在Yii2中将2个模型加载到单个视图中

[英]How to load 2 models into a single view in Yii2

Problem 问题
This problem is regarding Yii2 project which should show relevant Recipient list when click on the User Group name. 这个问题与Yii2项目有关,当单击用户组名称时,该项目应该显示相关的收件人列表。
At the moment I have created all the databases, models, CRUD generations with gridviews & working perfectly. 目前,我已经使用gridviews创建了所有的数据库,模型,CRUD代,并且运行良好。
But the problem is when I click on the grid item it navigates to another view (the default way) as following pic which I want to load in a part of the same view. 但是问题是当我单击网格项目时,它导航到另一个视图(默认方式),如下图所示,我想在同一视图的一部分中加载该图片。

在此处输入图片说明


User-groups (view -> index.php) 用户组 (视图-> 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&params="+(this.id);',
                    ];  
                },

        'columns' => [
            'group_id',
            'group_name',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
    <?php Pjax::end(); ?>

I have given the rowOptions in the GridView so that the record can be clicked & navigate to the recipient list through the URL. 我在GridView提供了rowOptions ,以便可以单击记录并通过URL导航到收件人列表。 Then that will be captured from the controller & filter the result according to group_id & render the view. 然后,将从控制器捕获并根据group_id过滤结果并呈现视图。


What I want 我想要的是

在此处输入图片说明

I want to dynamically load the recipient list when click on the left side group list. 单击左侧的组列表时,我想动态加载收件人列表。
I have tried 我努力了

  • iFrames but it is not working properly. iFrames但无法正常工作。 it works as a separate tab in the browser. 它在浏览器中充当单独的标签。
  • added many Yii navigation extensions like sideNavs by kartik for group list. 为组列表添加了许多Yii导航扩展,例如sideNavs But the problem was it haven't the ability to getting data from database. 但是问题是它没有从数据库中获取数据的能力。 when I create a group it didn't show in the list. 创建群组时,该群组未显示在列表中。 (It is good if the side nav was static) (如果侧面导航是静态的,则很好)

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... 所以..我想知道的是,是否有任何方法可以使用控制器执行此操作,或者是否有任何Jquery代码来支持此操作...

any suggestions or references are warmly welcome. 热烈欢迎任何建议或参考。

you can use jQuery .load for this. 您可以为此使用jQuery .load

lets say your recipient list container div has a id of #recipient-list so all you have to do is add the following on your onclick event: 假设您的收件人列表容器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

When you click in the row, on click event you will make an ajax request to retrieve the data for the second grid (Recipient List) 当您在该行中单击时,单击事件将发出ajax请求,以检索第二个网格的数据(收件人列表)

on success or complete of ajax request you will call $.pajax.reload to refresh the data in second grid like this 在成功或完成ajax请求后,您将调用$ .pajax.reload来刷新第二个网格中的数据,如下所示

$.pjax.reload({container:'#recipient-list'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM