YII2将从一个网格中选择的记录插入另一个网格

时间:2016-04-10 01:12:39

标签: php gridview yii2 crud

我是Yii2中的新手,之前我使用的是自制mvc-php + extjs 4.2

所以我现在正在Yii2上工作,我开始为简单的CRUD开始了它,当我需要为父子表创建输入表格时,我的pobrem开始了。 我有3张桌子

  1. 佣金
      

    commission_id
      佣金名称   佣金数量   commission_percent

  2. commission_scheme
      

    cscheme_id
      cscheme_name
      cscheme_amount
      cscheme_percent
      cscheme_bonus
      cscheme_description

  3. commission_scheme_detail
      

    cscheme_detail_id
      commission_id
      cscheme_id

  4. 参赛表格适用于commission_scheme和commission_scheme_detail。

    • 我使用gii
    • 生成了所有的crud
    • 我修改_form.php,在活动表单后添加了commission_scheme_detail的gridview
    • 我还添加了包含佣金选择网格的模式

    这是_form.php
    gridview的

    <?php Pjax::begin(['id' => 'pjaxCschemedetail', 
                                    'timeout' => false,
                                    'enablePushState' => false, 
                                    'clientOptions' => ['method' => 'POST']
                                    ]); 
                echo GridView::widget([
                    'id'        => 'gridCschemedetail',
                    'dataProvider' => $detailData,
                    'columns' => [
                        ['class' => 'yii\grid\SerialColumn'],
    
                        'CSCHEME_DETAIL_ID',
                        'CSCHEME_ID',
                        'COMMISSION_ID',
    
                        [
                            'class' => 'yii\grid\ActionColumn',
                            'buttons' => [
                            'update' => function ($url, $model) {
    
                                        return Html::a('<span class="glyphicon glyphicon-pencil"></span>', Yii::$app->urlManager->createUrl(['commission-scheme-detail/view','id' => $model->CSCHEME_DETAIL_ID,'edit'=>'t']), [
                                                                'title' => Yii::t('yii', 'Edit'),
                                                              ]);}
    
                            ],
                        ],
                    ],
                    'responsive'=>true,
                    'hover'=>true,
                    'condensed'=>true,
                    'floatHeader'=>true,
    
    
    
    
                    'panel' => [
                        'type'=>'info',
                        'before'=>  
                                                                                                                                                              'after'=>Html::a('<i class="glyphicon glyphicon-repeat"></i> Reset List', ['index'], ['class' => 'btn btn-info']),
                                    Html::button('Add', [
                                        'id'    => 'addDetailButton',
                                        'class' => 'glyphicon glyphicon-plus btn btn-success btn-ajax-modal',
                                        'value' => Url::to('@web/commission/listselect'),
                                        'data-target' => '#modal_cschemedetail',
                                    ]), 
    
                        'showFooter'=>false
                    ],
                ]); Pjax::end(); 
    

    模态

    Modal::begin([
                    'id' => 'modal_cschemedetail',
                    'header' => '<h4>Category</h4>',
                ]);
                echo '<div id="modal-content"></div>';
                echo Html::button('Add Selected', [
                                        'id'    => 'addCommissionsButton',
                                        'class' => 'btn btn-success btn-ajax-modal'
                                    ]);
                Modal::end();
                ?>
                </div>
            </div>
            <div class="box-body">
        </div>
    </div>
    

    选择网格

    <?php Pjax::begin(); echo GridView::widget([
        'id'        =>  'gridCommissionSelection',
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\CheckboxColumn'],
            ['class' => 'yii\grid\SerialColumn'],
    
            [
                'attribute' => 'COMMISSION_ID',
                'visible'   =>  false
            ],
            'COMMISSION_NAME',
            [
                'attribute' => 'TRANSACTION_TYPE_ID',
                'value' => 'transactionTypeName'
            ],
            'COMMISSION_DESC:ntext',
            'COMMISSION_AMOUNT',
        ]
        'responsive'=>true,
        'hover'=>true,
        'condensed'=>true,
        'floatHeader'=>true,
    
    ]); Pjax::end(); ?>
    


    是否可以将所选记录从模态插入到详细网格视图中,然后在用户单击创建按钮后,将表格和gridview中插入的记录保存到各自的表中?

    如何将所选记录插入gridview(不是db表,用户点击创建后会完成)?

1 个答案:

答案 0 :(得分:0)

我建议你简化流程。您可以在Modal轻松保存模型(执行插入/更新),然后刷新页面或调用$.pjax.reload('#gridCommissionSelection')以使用新条目重绘网格视图。

另一种方法是使用JS附加新行,然后通过AJAX请求保存它们。这将是相当肮脏和不明确的解决方案,我想:)