[yii2] How to Change a GridView Filter from GET to POST

In yii1, CGridView, by default, if we perform filters from data using the POST method to request data.

While on yii2, the default of the GridView filter uses the GET method. Some users may feel uncomfortable with the GET method for a variety of reasons.

To change GridView on yii2 from GET to POST method, you can try it like the following example.

1. View

In the view file add the Pjax widget before and after the grid-like this.

	..............
	..............
	..............
	
    <?php yiiwidgetsPjax::begin(['id' => 'some-id-you-like',

'timeout' => false,

'enablePushState' = > false,

'clientOptions' => ['method' => 'POST'] ]); 
		
?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' = > $searchModel,
        'columns' => [
            ..............
			..............
            ..............
        ],
    ]); 
	?>

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

..............
	..............
	..............

2. Controller

On the switch controller

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

with

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

It’s easy enough, isn’t it???? Good luck…

TOP TUTORIALS:  Prevent Windows 10 Specific Updates

Leave a Comment

Your email address will not be published. Required fields are marked *