2017-02-10 15 views
1

フィールドとボタン付きの検索フォームが1つあります。ボタンをクリックして、私は2 searchmodel - sellitembtdtSearchpuritembtdtSearchを検索したいです。結果は1つのビューで表示されます。私は問題なくビューを表示できます。問題は、私が検索している検索モードが1つだけであることです。どのようにsametimeでsearchModelを検索することができますか教えてください。2つのモデルからの2つのモデルからの2つのモデルyii2の単一フィールドによる検索

最初に、私は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

このコードは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

彼らは共通のフィールドを持っていますが、私が望むように2つの別個のグリッドビューを表示するには、別々の検索モデルを作成したいと思います。 – Tanmay

答えて

1

あなたのフォームでsearchModel2を使用しているため、searchModel2の結果が表示されています。また、searchModel1の結果を取得しません。

  • あなたのできることは、あなたの_searchファイルにsearchModel1を送信することです。

    <?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')); ?> 
    
  • は今、その私たちは2つのモデルのフィールドを持っている私たちは、これは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と正常に動作します!

関連する問題