yii2

时间:2017-02-10 11:34:08

标签: yii2

我有一个包含字段和按钮的搜索表单。点击按钮,我想搜索2个搜索模型 - sellitembtdtSearchpuritembtdtSearch。结果将在一个视图中显示。我可以毫无问题地显示视图。问题是当我只搜索一个搜索模型时会被搜索到。请让我知道如何同时搜索searchModel。

首先,我登陆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。感谢。

1 个答案:

答案 0 :(得分: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);
          });
       ');?>
    

完全尝试这个..测试并且工作正常!!