简体   繁体   English

从select2获取数据并将其传递给yii2中的控制器

[英]Get data from select2 and pass it to controller in yii2

I've one select2 form field, two datepickers and a search button in productnames index file. 我在productnames索引文件中有一个select2表单字段,两个datepicker和一个搜索按钮。 It is only to search data I'm unable to get the data selected in the select2 widget and pass it to controller which in turn can search other models. 只是搜索数据,我无法获取在select2小部件中选择的数据并将其传递给控制器​​,该控制器又可以搜索其他模型。

index.php index.php

<?php

use yii\helpers\Html;
use yii\grid\GridView;
use kartik\select2\Select2;
use dosamigos\datepicker\DatePicker;
use yii\helpers\ArrayHelper;
use frontend\modules\sbbtdtproduct\models\Productnames;
use yii\helpers\Json;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\sbbtdtproduct\models\ProductnamesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Productnames';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="productnames-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <div class="row">
        <div class="form-group">
            <div class="col-xs-5 col-sm-5 col-lg-5" >
                <?php
                    echo Select2::widget([
                    'model' => $model,
                    'attribute' => 'productnames_productname',
                    'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
                    'options' => ['placeholder' => 'Select Product'],
                    'pluginOptions' => [
                        'allowClear' => true
                    ],
                    //'productname' => $productname,
                    ]);
                ?>
            </div>
            <div class="col-xs-3 col-sm-3 col-lg-3">
                <?= DatePicker::widget([
                    'name' => 'Start Date',
                    'attribute' => 'from_date',
                    'value' => '2014-01-31',
                    'template' => '{addon}{input}',
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-mm-dd'
                        ]
                ]);?>
            </div>
            <div class="col-xs-3 col-sm-3 col-lg-3">
                <?= DatePicker::widget([
                    'name' => 'End Date',
                    'attribute' => 'to_date',
                    'value' => '2014-01-31',
                    'template' => '{addon}{input}',
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-mm-dd'
                        ]
                ]);?>
            </div>
            <div class="col-xs-1 col-sm-1 col-lg-1" >

                <?= Html::a('Search', ['/sbbtdtproduct/production/index','productname' => $model['productnames_productname']], ['class'=>'btn btn-primary']) ?>
            </div>           
        </div>
    </div>
</div>

production controller action 生产控制员行动

public function actionIndex($productname)
    {
        $productname = yii::$app->request->get('productnames_productname');
        $searchModel = new ProductionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $productname);

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

productionsearch model 生产搜索模型

public function search($params,$productname)
    {
        $query = Production::find()
                ->where(['productname' => $productname]);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'productionid' => $this->productionid,
            'productiondate' => $this->productiondate,
            'itemid' => $this->itemid,
            'prodqty' => $this->prodqty,
        ]);

        $query->andFilterWhere(['like', 'productname', $this->productname])
            ->andFilterWhere(['like', 'batchno', $this->batchno]);

        return $dataProvider;
    }

error - 错误- 在此处输入图片说明 update - 更新- 在此处输入图片说明 Database Log 数据库日志 在此处输入图片说明 The data base Log shows that no value has been passed from search model. 数据库Log显示未从搜索模型传递任何值。

I can see the value selected in select2 or datepicker as below, but it's not passing to the controller. 我可以看到在select2或datepicker中选择的值,如下所示,但它没有传递给控制器​​。

在此处输入图片说明

在此处输入图片说明

1.on your view page You have not added form tag, so other parameters will not get posted, you must add everything inside form tag and submit that form, as follows 1.在视图页面上,您尚未添加form标签,因此其他参数将不会发布,您必须在form标签中添加所有内容并submit该表单,如下所示

<?php

use yii\helpers\Html;
use yii\grid\GridView;
use kartik\select2\Select2;
use dosamigos\datepicker\DatePicker;
use yii\helpers\ArrayHelper;
use frontend\modules\sbbtdtproduct\models\Productnames;
use yii\helpers\Json;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\sbbtdtproduct\models\ProductnamesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Productnames';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="productnames-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); 
    $form = ActiveForm::begin([
                'action' => ['/sbbtdtproduct/production/index'],
                'method' => 'post',
                'options' => ['data-pjax' => true],
                'enableClientValidation' => FALSE
    ]);
    ?>
    <div class="row">
        <div class="form-group">
            <div class="col-xs-5 col-sm-5 col-lg-5" >
                <?php
                    echo Select2::widget([
                    'model' => $model,
                    'attribute' => 'productnames_productname',
                    'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
                    'options' => ['placeholder' => 'Select Product'],
                    'pluginOptions' => [
                        'allowClear' => true
                    ],
                    //'productname' => $productname,
                    ]);
                ?>
            </div>
            <div class="col-xs-3 col-sm-3 col-lg-3">
                <?= DatePicker::widget([
                    'name' => 'Start Date',
                    'attribute' => 'from_date',
                    'value' => '2014-01-31',
                    'template' => '{addon}{input}',
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-mm-dd'
                        ]
                ]);?>
            </div>
            <div class="col-xs-3 col-sm-3 col-lg-3">
                <?= DatePicker::widget([
                    'name' => 'End Date',
                    'attribute' => 'to_date',
                    'value' => '2014-01-31',
                    'template' => '{addon}{input}',
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-mm-dd'
                        ]
                ]);?>
            </div>
            <div class="col-xs-1 col-sm-1 col-lg-1" >
                <?= Html::submitButton('Search', ['class'=>'btn btn-primary']) ?>
                 <?php ActiveForm::end(); ?>
            </div>           
        </div>
    </div>
</div>

2.Controller 2.控制器

now inside your controller you can access parametes as follows 现在,在控制器内,您可以按以下方式访问参数

public function actionIndex()
    {
        $productname = Yii::$app->request->post('productnames_productname');
        $searchModel = new ProductionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $productname);

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

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

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