2017-02-10 22 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和正常工作!