[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:  How to record a Windows 10 screen without apps
Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

I Have Disabled The AdBlock, Reload Now
Powered By
CHP Adblock Detector Plugin | Codehelppro