从表单字段获取数据到yii2中的按钮值

时间:2017-02-09 14:42:24

标签: yii2

我在表单中有一个表单字段和一个按钮。该按钮应从表单字段中获取数据并将值作为参数传递。请告诉我如何从表单字段中获取数据并传递值。

表单字段 -

<?= $form->field($model, 'productname')->textInput(['maxlength' => true]) ?>

按钮

<?= Html::a('Search', ['/stock/sellitem/printproductledger3', 'productname' => '8904187001305'], ['class'=>'btn btn-primary']) ; ?>

上面的示例工作正常,因为此处已经给出了值'productname' => '8904187001305' 我需要做的是从表单字段中获取值'8904187001305'。请帮忙。

的actionIndex

public function actionIndex2()
    {
        $searchModel = new SellitemsbdtSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

此索引导致_formdtbt

'action' => ['/stock/sellitem/printproductledger3',],

<?= $form->field($model, 'productname')->textInput(['maxlength' => true,]) ?>
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>

控制器操作printproductledger3

public function actionPrintproductledger3() {

        $searchModel1  = new SellitemsbdtSearch();
        $dataProvider1 = $searchModel1->search(Yii::$app->request->get());
        $searchModel2  = new PuritemsbdtSearch();
        $dataProvider2 = $searchModel2->search(Yii::$app->request->get());
 return $this->render('_printproductledgerbtdt', [
            'dataProvider1' => $dataProvider1,
            'searchModel1'  => $searchModel1,
            'searchModel2' => $searchModel2,          
            'dataProvider2' => $dataProvider2,
        ]);
    }

嗯,我想我已指出错误的问题了。问题是模型sellitemsbdtSearch正在过滤,但puritemsbdtSearch未过滤。

搜索模型sellitemsbdtSearch

public function search($params)
    {
        //$query = Sellitem::find();
        $query = Sellitem::find()
                ->joinWith(['siSs'])
                ->select(['sellsum.ss_date as date','si_iupc','si_idesc', 'concat("By sales to Tax Invoice ", sellsum.ss_invno) as particular', 'si_qty as sellqty','(si_qty * si_rate) as value'])
                ->orDerBy([
                        'sellsum.ss_date'=>SORT_DESC,
                    ]);
        // add conditions that should always apply here

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

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        if($this->productname){
            $query->andFilterWhere(['si_iupc'=> $this->productname]);    
            return $dataProvider;
        }
}

搜索模型puritemsbdtSearch

public function search($params)
    {
        $query = Puritem::find()
                ->joinWith(['psi'])
                ->select(['pursum.ps_date as date','pi_upc','pi_desc', 'concat("By purchase to Tax Invoice ", pursum.ps_invno) as particular', 'pi_qty as buyqty','(pi_qty * pi_rate) as value'])
                ->orDerBy([
                        'pursum.ps_date'=>SORT_DESC,
                    ]);

        // add conditions that should always apply here

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

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        if($this->productname){
            $query->andFilterWhere(['pi_upc'=> $this->productname]);    
            return $dataProvider;
        }
}

1 个答案:

答案 0 :(得分:1)

您可以使用这种方式将模型的值发送到您的操作

 <?php $form = ActiveForm::begin([
      'action' => ['your_action'],
      'method' => 'your_method ',   /// get or post
  ]); ?>
    <?= $form->field($model, 'productname')->textInput(['maxlength' => true]) ?>

  <div class="form-group">
      <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
      <?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
  </div>

  <?php ActiveForm::end(); ?>

一旦提交,你就无法以normale yii2的方式获得价值

你可以看到更多.. http://www.yiiframework.com/doc-2.0/guide-input-forms.html

对于第二个没有相同realetd模型的模型,并将其归档为第一个应该使用

的模型
// in $get try retrive the content of $get (in this case i related  your yourModel1)
$get = Yii::$app->request->get(['yourModel1'])
// and the use the value for searching in your model2 with the proper value  
// obviously you should adatp the name in this sample  with  the proper ones
$searchModel2  = new PuritemsbdtSearch();
    $dataProvider2 = $searchModel2->search(['YourModelSearch2Name'=>['yourAttributeYouNeed'=>$get['productname']]]);