GridView filterModel and search form simultaneously

Hi,

I have a problem using the GridView filterModel and a separate search form side by side (same search model).

If I filter data in the GridView header it works. But when I submit the search form, previous filters will be overwritten.

I can avoid this behavior by not specifying the action element of the form. But this causes an linear increase of the the url.

Does anybody had this problem before and could provide me a solution for this problem?

My goal is to use GridView filters and search form with a static length of the url.

Thank you :)

No one has a clue?

Could you post the relevant code, please?

Hi Patrick!

Here is the relevant code:




// Controller

class OrdersController extends ApplicationController 

{

    public function actionIndex()

    {

        $searchModel = new SupplierOrdersSearch();

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

		

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

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }

}






// View

$form = ActiveForm::begin([

	'id' => 'orders-search-form',

	'method' => 'GET',

	'action' => Url::to(['index']),

]);

echo $form->field($searchModel, 'show_closed')->checkbox(['label' => 'Show closed orders'], true);

echo Html::submitButton('Submit', ['class' => 'btn btn-primary'])

$form->end();


echo GridView::widget([

	'dataProvider' => $dataProvider,

	'filterModel' => $searchModel,

	'columns' => [

		// columns

	]

]);



When I remove the ‘action’ => Url::to([‘index’]) from the ActiveForm::begin the form search and the gridview header search work side by side, but the url is increasing.

When I run it like show in the example, the url is not increasing, but if I submit the form, the header search parameters will be overwritten.

I hope you can understand my issue.

If you have further questions just ask. I am thankful for every hint :)

How is the url increasing? Is the same parameter contained more than once?

btw: You don’t need to use “Url::to” for the form action, it’ll be applied anyway:

http://www.yiiframework.com/doc-2.0/yii-widgets-activeform.html#$action-detail

Yes. The URL is increasing with the same parameters over and over again.

If I don’t use the “Url::to” in the form action, the form submit will append the url.

As you have noticed, you need to set ‘action’ parameter of the search form to “Url:to([‘index’])” (or, just “[‘index’]”) in order to keep the url from getting long.




$form = ActiveForm::begin([

        'id' => 'orders-search-form',

        'method' => 'GET',

        'action' => Url::to(['index']),

        /* 'action' => ['index'], */ /* this is OK, too */

]);



This is because ActiveForm will set its action to the current url by default. Once the search result has been displayed, then the current url should contain the search parameters in it. So we have to set the url without the search parameters explicitly.

It’s not possible to avoid the synchronization of the search parameters between the search form and the gridview filter, as long as they share the same search model. And IMO it is also what users expect. I don’t want the parameters with the same name having different values in the form and in the filter.

But, I think you may consider having hidden fields in the search form. They will preserve the search parameter values of the filter. :)