Search code examples
yii2

One search form to 3 Gridview in same page


Friends,

I have an INDEX page with 3 GRIDVIEW components (model Keys, model Products, model Indicators). And on that same page I have a search form with a dropdownlist (company).

I need to: When you select for example company 02 the 3 GRIDVIEW are filtered only company records 02.

_search.php

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\date\DatePicker;
?>

<div class="ideas-search">

    <?php $form = ActiveForm::begin([
        'options' => [
                    'class' => 'form-inline',
                    ],
        'action' => ['index'],
        'method' => 'get',
    ]); ?>

    <?= $form->field($model, 'pa')->dropdownList([..])?>

...

index.php

<?php echo $this->render('_search', ['model' => $searchBase]); ?>

        <?php Pjax::begin(['id' => '1']) ?>
        <?= GridView::widget([
            'id' => 'grid1',
            'dataProvider' => $dataProviderKey,
            'columns' => [
                ...
            ],
        ]); ?>
        <?php Pjax::end() ?>

        <?php Pjax::begin(['id' => '2']) ?>
        <?= GridView::widget([
            'id' => 'grid2',
            'dataProvider' => $dataProviderProduct,
            'columns' => [
                ...
            ],
        ]); ?>
        <?php Pjax::end() ?>

        <?= GridView::widget([
            'dataProvider' => $dataProviderIndicators,
            'summary' => false,
            'columns' => [
                ...
            ],
        ]); ?>

== UPDATE==

BaseController

public function actionIndex()
{
    $searchBase = new BaseSearch();
    $searchBase->pa = 0;
    $searchBase->data = date("Y-m-d");
    $dataProviderBase = $searchBase->search(Yii::$app->request->queryParams);

    $searchMobilizadores = new MobilizadoresSearch();
    $dataProviderMobilizadores = $searchMobilizadores->search(Yii::$app->request->queryParams);

    $searchIndicadores = new IndicadoresSearch();
    $dataProviderIndicadores = $searchIndicadores->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchBase' => $searchBase,
        'dataProviderBase' => $dataProviderBase,
        'searchMobilizadores' => $searchMobilizadores,
        'dataProviderMobilizadores' => $dataProviderMobilizadores,
        'searchIndicadores' => $searchIndicadores,
        'dataProviderIndicadores' => $dataProviderIndicadores,
    ]);
}

Solution

  • dont forget define filterModel in your Gridview Widget

    view file

    GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
    

    1st Approach :

    You may use a single model that extend from model that joint all 3 table and create a search model for it. If u insist on separating all 3 table in the view, you may just declare different column for each grid. a tip to achieve this, you may create a view and use gii tool to generate view file.

    the tricky part is that 1 model can only hold one table except you joint. But, by joining table, you may get extra rows that are not necessarily needed for lets say your $dataProviderKey grid.

    2nd Approach :

    manually check for params from Yii::$app->request->get() add filter for each $dataprovider.

    controller file

    if(isset($id = Yii::$app->request->get('company_id'))) {
        $dataProviderKey->query->andFilterWhere(['company.id' => $id]);
        $dataProviderProduct->query->andFilterWhere(['company.id' => $id]);
        $dataProviderIndicators->query->andFilterWhere(['company.id' => $id]);
    }
    

    == UPDATE ==

    to answer your update question, you may assign your base search attributes to the respective attributes from others searchModel like this :

    public function actionIndex()
    {
        $searchBase = new BaseSearch();
        $searchBase->pa = 0;
        $searchBase->data = date("Y-m-d");
        $dataProviderBase = $searchBase->search(Yii::$app->request->queryParams);
    
        $searchMobilizadores = new MobilizadoresSearch();
        $searchMobilizadores->company = $searchBase->company; -->add this
        $dataProviderMobilizadores = $searchMobilizadores->search(Yii::$app->request->queryParams);
    
        $searchIndicadores = new IndicadoresSearch();
        $searchIndicadores->company = $searchBase->company; -->add this
        $dataProviderIndicadores = $searchIndicadores->search(Yii::$app->request->queryParams);