Custom Search form in Yii2 GridView

How I can make Custom Search form in Yii2 GridView,

Let user search some values and gridview reload automatically with user searched data?

Hello Muhammad Shahzad

DO you got answer if yes then post here.

Thanks

Not get any answer yet :)

Create CRUD using gii, then it will make a search form ("_search.php").

Customize it as you like.

Thank I will try, I want to implement Pjax search form.

Well, it’s quite easy to modify a gii-generated index page into a pjax powered page.

  1. Wrap the grid view or list view with "<?php Pjax::begin(); ?>" and "<?php Pjax::end(); ?>"



// index.php view file

    <h1><?= Html::encode($this->title) ?></h1>

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

    <?php Pjax::begin(); ?>

    <?= GridView::widget([

        ...

    ]); ?>

    <?php Pjax::end(); ?>



  1. Optionally you may optimize the actionIndex method in your controller.



    public function actionIndex()

    {

        $searchModel = new ModelSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        if (Yii::$app->request->isPjax) {

            return $this->renderPartial('index', [

                'searchModel' => $searchModel,

                'dataProvider' => $dataProvider,

            ]);

        } else {

            return $this->render('index', [

                'searchModel' => $searchModel,

                'dataProvider' => $dataProvider,

            ]);

        }

    }



Thanks I will try.

Hello Muhammad Shahzad,

Have you tried the solution so far?

I have the same issue.

I already have the list of links for type and category. I just want to know how can I display the gridview when I click the link.

thanks!

Hi,

have any ideas how to make a search after you join more models, because I fund out how to connect models and echo related data, but don’t know how to do search in new columns.

Tx. in advance!

It is an old post, but who knows…

This How-to could help to solve this.

http://www.yiiframework.com/wiki/772/pjax-on-activeform-and-gridview-yii2/

Controller action could look like this:





    public function actionIndex()

    {

        $searchModel = new MyModelSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        // if(Yii::$app->request->getHeaders()->has('X-PJAX'))

        if (Yii::$app->request->isPjax) 

        {

            // return $this->renderPartial('index', [

            return $this->renderAjax('index', [

                'searchModel' => $searchModel,

                'dataProvider' => $dataProvider,

            ]);

        }

        else

        {

            return $this->render('index', [

                'searchModel' => $searchModel,

                'dataProvider' => $dataProvider,

            ]);

        }

        

    }




Don’t forget this in _form.php




$this->registerJs(

   '$("document").ready(function(){ 

        $("#new_country").on("pjax:end", function() {

            $.pjax.reload({container:"#countries"});  //Reload GridView

        });

    });'

);