簡體   English   中英

在yii2中從表單字段獲取數據到按鈕值

[英]Get data from form field to button value in 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;
        }
}

您可以使用這種方式將模型的價值傳遞給您的行動

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

一旦提交,您將無法以法線yii2的方式獲取該值

您可以查看更多。.http ://www.yiiframework.com/doc-2.0/guide-input-forms.html

對於第二個模型,該模型沒有與第一個模型相同的真實模型,並且已歸檔,您應該使用

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