简体   繁体   English

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

[英]Get data from form field to button value in yii2

I have a form field and a button in a form. 我有一个表单字段和一个表单中的按钮。 The button should get the data from the form field and pass the value as parameter. 该按钮应从表单字段获取数据并将值作为参数传递。 Please tell me how can I get the data from the form field and pass the value. 请告诉我如何从表单字段获取数据并传递值。

form field - 表格栏位-

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

button 纽扣

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

The above example work fine because the value is already given here 'productname' => '8904187001305' What I need to do is get the value '8904187001305' from the form field. 上面的例子很好用,因为这里已经给定了值'productname' => '8904187001305'我需要做的是从表单字段中获取值'8904187001305' Please help. 请帮忙。

actionIndex actionIndex

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

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

This index leads to _formdtbt 该索引导致_formdtbt

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

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

Controller action printproductledger3 控制器动作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,
        ]);
    }

Well, I think I've pointed to wrong issue. 好吧,我想我指出了错误的问题。 The issue is model sellitemsbdtSearch is getting filtered but puritemsbdtSearch is not getting filtered. 问题在于模型sellitemsbdtSearch已被过滤,而puritemsbdtSearch未得到过滤。

Search Model sellitemsbdtSearch 搜索模型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;
        }
}

Search Model puritemsbdtSearch 搜索模型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;
        }
}

You could use this way for sending the value of the model to your action 您可以使用这种方式将模型的价值传递给您的行动

 <?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(); ?>

once submit you cant get the value in the normale yii2 way 一旦提交,您将无法以法线yii2的方式获取该值

you can see this for more .. http://www.yiiframework.com/doc-2.0/guide-input-forms.html 您可以查看更多。.http ://www.yiiframework.com/doc-2.0/guide-input-forms.html

for the second Model that don't have the same realetd model and filed as the first you should use 对于第二个模型,该模型没有与第一个模型相同的真实模型,并且已归档,您应该使用

// 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']]]);

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

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