2017-02-10 73 views
1

我有一個帶有字段和按鈕的搜索表單。在按鈕上單擊我要搜索2 searchmodel - sellitembtdtSearchpuritembtdtSearch。結果將顯示在一個視圖中。我可以毫無問題地顯示視圖。問題是,當我只搜索一個searchmodel正在搜索。請讓我知道如何在同一時間搜索searchModel。yii2中單個字段的兩個模型搜索中的兩個gridview

首先我登陸index2.php頁面,在那裏的表單是。

'action' => ['/stock/sellitem/printproductledger3',], 
'method' => 'get', 
<?= $form->field($model, 'productname')->textInput(['maxlength' => true,]) ?> 
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?> 

actionIndex2.php

public function actionIndex2() 
    { 
     $searchModel1 = new SellitemsbdtSearch(); 
     $dataProvider1 = $searchModel1->search(Yii::$app->request->queryParams); 
     $searchModel2 = new PuritemsbdtSearch(); 
     $dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams); 

     return $this->render('_formbtdt', [ 

      'model' => $searchModel2, 
      //'searchModel1' => $searchModel2, 
     ]); 
    } 

public function actionPrintproductledger3() { 

     $searchModel1 = new SellitemsbdtSearch(); 
     $dataProvider1 = $searchModel1->search(Yii::$app->request->get()); 
     $searchModel2 = new PuritemsbdtSearch(); 
     $dataProvider2 = $searchModel2->search(Yii::$app->request->get()); 
     //$productname = yii::$app->request->get('productname'); 
     //$prodesc = yii::$app->request->get('prodesc'); 

     $content = $this->renderPartial('_printproductledgerbtdt', [ 
      //'productname' => $productname,    
      'searchModel1' => $searchModel1,   
      'dataProvider1' => $dataProvider1, 
      'searchModel2' => $searchModel2,   
      'dataProvider2' => $dataProvider2,    
      //'prodesc' => $prodesc, 

      ]); 
return $this->render('_printproductledgerbtdt', [ 
      'dataProvider1' => $dataProvider1, 
      'searchModel1' => $searchModel1, 
      'searchModel2' => $searchModel2,   
      'dataProvider2' => $dataProvider2, 
     ]); 

enter image description here

此代碼僅searchs puritemdtdtSearch。我想同時搜索puritembtdtSearchsellitembtdtSearch。謝謝。

+0

你有沒有考慮過使用ajax?你是什​​麼意思,你需要搜索不同的searchModels ...你可以只搜索1 searchModel,如果他們是相關的使用關係 –

+0

是的。這裏 - http://stackoverflow.com/questions/41900399/display-pdf-in-browser-by-calling-controller-action-by-ajax-in-yii2 ... – Tanmay

+0

他們有一個共同的領域,但因爲我想要顯示兩個單獨的gridview,我想構建單獨的搜索模型。 – Tanmay

回答

1

問題是你在表單中使用searchModel2,所以你只是得到searchModel2的結果。並沒有得到searchModel1的結果。

  • 你可以做的是發送searchModel1到_search文件。

    <?php echo $this->render('_search', ['model1' => $searchModel1, 'model2' => $searchModel2]); ?> 
    
  • 現在在您的表單中使用隱藏輸入。

    <?php echo $form->field($model1, 'productName')->textInput(['id' => 'model1']); ?> 
        <?php echo $form->field($model2, 'prodcutName')->hiddenInput(array('id' => 'model2')); ?> 
    
  • 現在它,我們有兩個示範田,我們需要來填充隱藏字段,這可以使用jQuery來完成。

    <?php $this->registerJs(' 
         //add the .search class name for your search button 
         jQuery("body").on("click", ".search", function() { 
         alert("Hello"); 
         var a = $("#model1").val(); 
         $("#model2").attr("value", a); 
         }); 
        ');?> 
    

嘗試完全this..tested和正常工作!