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

时间:2016-11-22 08:25:09

标签: yii2

我在产品名称索引文件中有一个select2表单字段,两个日期选择器和一个搜索按钮。只搜索数据我无法获取select2小部件中选择的数据并将其传递给控制器​​,控制器又可以搜索其他模型。

的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>

生产控制器操作

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,
        ]);
    }

生产搜索模型

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;
    }

错误 - enter image description here 更新 - enter image description here 数据库日志 enter image description here 数据库日志显示没有从搜索模型传递任何值。

我可以看到select2或datepicker中选择的值如下所示,但它没有传递给控制器​​。

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

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

现在在您的控制器中,您可以按如下方式访问参数

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,
        ]);
    }
相关问题