Yii2 使用gridview中的kartik Select2小部件筛选数据

Yii2 使用gridview中的kartik Select2小部件筛选数据,yii2,Yii2,我在索引页面的顶部添加了一个kartik select2小部件。我的目的是在同一个页面中用相同的选择值同时过滤2个gridview。 我已经编写了jquery,json结果(警报)显示良好。但无法在gridview中获取结果。请帮忙。请让我知道在这方面是否有更好的方法。 My index.php文件- <?php use yii\helpers\Html; use yii\grid\GridView; use kartik\select2\Select2; use yii\helpers

我在索引页面的顶部添加了一个kartik select2小部件。我的目的是在同一个页面中用相同的选择值同时过滤2个gridview。 我已经编写了jquery,json结果(警报)显示良好。但无法在gridview中获取结果。请帮忙。请让我知道在这方面是否有更好的方法。 My index.php文件-

<?php

use yii\helpers\Html;
use yii\grid\GridView;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper;
use frontend\modules\productstockbook\models\Productnames;
use yii\helpers\Json;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\productstockbook\models\ProductionSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Productions';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="production-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <?php

        //echo $list=CHtml::listData(Productnames::model()->findAll(), 'productnames_productname', 'productnames_productname');

        //$model->productnames_productname = 2;
        echo Select2::widget([
        'model' => $model,
        'attribute' => 'productnames_productname',
        'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
        'options' => ['placeholder' => 'Select Product', 'id' => 'catid'],
        'pluginOptions' => [
            'allowClear' => true
        ],
        ]);
     // echo Select2::widget([
     //                        'attribute' => 'productnames_productname',
     //                        //'model' => $model,
     //                        'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
     //                        'options' => ['placeholder' => 'Pilih Guru...', 'id' => 'guru-id', 'class' => "form-control"],
     //                        'pluginOptions' => [
     //                            'allowClear' => true,
     //                            'theme' => \kartik\widgets\Select2::THEME_BOOTSTRAP
     //                        ],
     //                    ]);
    ?>
    <div class= 'col-md-6'>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            //'productionid',
            'productiondate',
            //'itemid',
            'productname',
            //'batchno',
            'prodqty',

            //['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
</div>

<div class='col-md-6'>

    <?php
        echo GridView::widget([
        'dataProvider' => $dataProvider2,
        'filterModel' => $searchModel2,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'billdate',
            'productsales_partyname',
            'productname',
            'total',

        ], 
        ]); 
      ?>
  </div>
</div>
<?php
$script = <<< JS
$('#catid').change(function(){   
    var catid = $(this).val();

     $.get('index.php?r=productstockbook/production/get-for-production',{ catid : catid }, function(data){
        alert(data);
        var data = $.parseJSON(data);
        //I'm missing this part
    });
});
JS;
$this->registerJs($script);
?>


按如下方式更改脚本

<?php
 $script = <<< JS
 $('#catid').change(function(){   
   var catid = $(this).val();
   // make http request as select value changes , 
   window.location = 'index.php?r=productstockbook/production/get-for-production&catId='+catid; 
});
JS;

$this->registerJs($script);
?>
通过包含新变量自定义两个搜索模型搜索函数

例如

public function search($params,$catId)
{
    $query = YourModel::find();

    // filter query object with the passed value.
    $query->andWhere(['your_search_column' => $catId]);



    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        // your filtering codes
    ]);

    return $dataProvider;
}

您想从select2值或筛选列值中筛选gridview??谢谢Arjun。工作得很好。除了window.location,我们还能做到这一点吗?因为它正在动态重定向到另一个页面,并且不能使用任何其他javascript代码。(我不太擅长)。请查看链接-@ck_arjunIf如果您不想为每次选择选项更改发送http请求,您可以使用Ajax并每次替换内容
public function actionIndex()
{   
    $catId = yii::$app->request->get('catId');

    $searchModel = new ProductionSearch();
    // pass catId to search model function
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams,$catId);

    $searchModel2 = new ProductsalesSearch();
    $dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams,$catId);

    $model = new Productnames();
    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'searchModel2' => $searchModel2,
        'dataProvider2' => $dataProvider2,
        'model' => $model,
    ]);
}
public function search($params,$catId)
{
    $query = YourModel::find();

    // filter query object with the passed value.
    $query->andWhere(['your_search_column' => $catId]);



    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        // your filtering codes
    ]);

    return $dataProvider;
}