【问题标题】:Get data from select2 and pass it to controller in yii2从 select2 中获取数据并传递给 yii2 中的控制器
【发布时间】:2016-11-22 08:25:08
【问题描述】:

我在产品名称索引文件中有一个 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;
    }

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

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

【问题讨论】:

  • 你检查过$productname = yii::$app-&gt;request-&gt;get('productnames_productname');的输出是什么`
  • 没有,先生。怎么办?
  • 在你的控制器动作里面做`var_dump(yii::$app->request->get('productnames_productname')); exit();` 并检查输出是什么
  • 添加了 var_dump(yii::$app->request->get('productnames_productname')‌​);出口();就在 $productname = yii::$app->request->get('productnames_productname'); 之后。 got - 语法错误,意外的 '‌​' (T_STRING)
  • 为什么在 var_dump 中有这个语法错误?我写对了吗?

标签: yii2


【解决方案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.控制器

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

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

【讨论】:

  • 实施此操作后,没有错误但没有获得所需的输出。该值未过滤。我已经在问题中添加了输出。
  • 嗨 Nitin,我没有得到想要的结果。输出。可以看看吗?
  • Hey Tanmay,你需要自己调试,很难猜到错误,使用页脚处的调试工具栏检查生成的查询是什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-07
  • 1970-01-01
  • 2019-11-01
  • 2021-12-26
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多