Yii 2 Gridview sorting header- add custom parameter in the url

592 views Asked by At

When I click on the column header of Grid view, it is getting sorted, and in the URL sort parameter is gettting passed like page/index?id=12&sort=column1. I want to add another parameter while sorting like this page/index?id=12&sort=column1&custom_id=2.

Please help me on this

1

There are 1 answers

1
Роман Агилов On

You can configure it in your DataProvider using sort params like this:

$dataProvider = new ActiveDataProvider([
    // ...
    'sort' => [
        'params' => ['custom_id' => 2]
    ]
]);

Note that according to this official documentation if you set params explicitly - sort links won't have request's context GET params.

So you if you want your filter to persist you should merge $_GET array with your custom params injected to the link.

The example with merged $_GET:

$contextParams = \Yii::$app->getRequest()->getQueryParams();

$dataProvider = new ActiveDataProvider([
    // ...
    'sort' => [
        'params' => array_merge($contextParams, ['custom_id' => 2])
    ]
]);