Yii2将数据从3 Gridview传递到DataBase

时间:2016-05-16 08:30:20

标签: php mysql yii2

我在一个页面中基于3个不同的模型回显了3个不同的gridview。模型没有联系。 我想用chechbox从每个网格中收集一些特定数据,并将它们发送到相同的数据库表中。 这是显示页面:

/** Operators Grid View  **/ 
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],

                    [ 'attribute' => 'useralias',
                    'label' =>'Operator Name',],

                    [ 'class' => 'yii\grid\CheckboxColumn',],

                    [ 'attribute' => 'currency',
                    'label' =>'Operator Currency',],

                    ['class' => 'yii\grid\ActionColumn', 'template' => '{view}'],
                    ],
]); ?>

    /** Country Grid View  **/
     <?= GridView::widget([
    'dataProvider' => $dataProviderCountry,
    'filterModel' => $searchModelCountry,
    'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],

                    [ 'class' => 'yii\grid\CheckboxColumn',],


                    [ 'attribute' => 'popis',
                    'label' =>'Country',],

                    ['class' => 'yii\grid\ActionColumn', 'template' => '{view}'],
                    ],
]); ?>
    /** Currency Grid View  **/

     <?= GridView::widget([
    'dataProvider' => $dataProviderCurrency,
    'filterModel' => $searchModelCurrency,
    'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],

                    [ 'attribute' => 'currency_name',
                    'label' =>'Currency Name',],
                    [ 'class' => 'yii\grid\CheckboxColumn',], 


                    [ 'attribute' => 'value',
                    'label' =>'Value',],

                    ['class' => 'yii\grid\ActionColumn', 'template' => '{view}'],
                    ],
]); ?>

这是来自控制器的索引函数:

public function actionIndex()
{
            //RMS
    $searchModel = new CardrmsSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            //Country
            $searchModelCountry = new CountrySearch();
    $dataProviderCountry = $searchModelCountry->search(Yii::$app->request->queryParams);
            //Currency      
            $searchModelCurrency = new CurrencySearch();
    $dataProviderCurrency = $searchModelCurrency->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
                    'searchModelCountry' => $searchModelCountry,
        'dataProviderCountry' => $dataProviderCountry,
                    'searchModelCurrency' => $searchModelCurrency,
        'dataProviderCurrency' => $dataProviderCurrency,
    ]);    }

我找不到任何相关内容,我的想法是创建一个收集数据的动作,但我不知道如何一步收集所有3个选中的复选框中的数据! 请问如何做到这一点?

1 个答案:

答案 0 :(得分:0)

首先以活动形式包裹您的网格。比每个网格中复选框的设置名称

[ 
  'class' => 'yii\grid\CheckboxColumn',
  'name'=>'anyname'

],

在任何地方设置表单操作

<?php $form = ActiveForm::begin([
        'action' => 'your-action'
    ]);

转储并查看该操作中的POST数据,您将获得表示每个网格的选定行的ID数组。 您可以按每个网格中的复选框名称区分数据。