Yii2: custom parameters in pagination not working

Created on 20 Sep 2016  路  6Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

Create a data array and add extra params in pagination ie
$dataProvider= new SqlDataProvider([
'sql'=>"$sql",
'totalCount' => (int)$count,
'params'=>[':q'=>$q],
'pagination' => [
'pageSize' => 20,
'params'=>['q'=>'test'],
],

    ]);

What is the expected result?

My pagination URL for second page should be index.php?r=search&page=2&q='test' where xxx is the parameter passed in the pagination.

What do you get instead?

index.php?r=search&page=2

Additional info

| Q | A |
| --- | --- |
| Yii version | 2.0.? |
| PHP version | 5.6 |
| Operating system | windows |

need more info

Most helpful comment

'params' => ['q'=>$q],

this will disable pagination, you have to add the page param too:

'params' => [
    'q' => $q,
    'page' => Yii::$app->request->get('page'),
],

or use the default value of params which will include all GET params by default.

All 6 comments

I am unable to reproduce the issue, added a test case to verify it works: e327d01

Please provide additional information to reproduce the problem.

Hi Cebe,

Thanks for looking into this. I've attached a sample code to show the issue
test.zip what i want to do is pass the parameter q in the pagination link so that the link become like ie test?q=david&page=2

Controller - backend/models/TestController.php
Model - backend/models/TestSearch.php
View- backend/views/test/index.php

TestController

<?php
namespace backend\controllers;

use Yii;
use backend\models\TestSearch;
use yii\web\Controller;

class TestController extends Controller
{

    public function actionIndex(){

        $q=yii::$app->request->get('q');
        $test=new TestSearch();
        $dataProvider=$test->doSearch($q);
        return $this->render('index', [
            'dataProvider' => $dataProvider,

        ]);
    }
} 

?>   

TestSearch Model

<?php
namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ArrayDataProvider;


class TestSearch extends Model
{
    public $q;

    public function rules()
    {
        return [
            [['q'], 'safe'],
        ];
    }

    public function doSearch($q){
        $q=trim($q);

        $resultData = [
            array("id"=>1,"name"=>"David Hagen"),
            array("id"=>2,"name"=>"Brad Pitt"),
            array("id"=>3,"name"=>"Darren Kerry"),
            array("id"=>3,"name"=>"Greg Stevenson"),
            array("id"=>3,"name"=>"Asif Iqbal"),
            array("id"=>3,"name"=>"Mohammed Akram"),
            array("id"=>3,"name"=>"Luiza Smith"),
            array("id"=>3,"name"=>"Duy Nuygen"),
            array("id"=>3,"name"=>"Jeff Ashdown"),
            array("id"=>3,"name"=>"Glenn Smith"),
            ];

        $dataProvider = new ArrayDataProvider([
            'key'=>'id',
            'allModels' => $resultData,
            'pagination'=>array(
                'pageSize'=>3,
                'params'  => ['q'=>$q],
            ),
        ]);

        return $dataProvider;
    }

}

views (test)

<?php
use yii\helpers\Html;
use yii\grid\GridView;

?>

<?php

echo GridView::widget([
    'dataProvider' => $dataProvider,

    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'id',
        [
            'attribute' => 'name',
            'value' => 'name',
        ],
    ]
]);

?>

'params' => ['q'=>$q],

this will disable pagination, you have to add the page param too:

'params' => [
    'q' => $q,
    'page' => Yii::$app->request->get('page'),
],

or use the default value of params which will include all GET params by default.

Thanks Cebe. But it doesn't work. the parameter q doesn't pass at well. It only pass page.

I put your example code into yii basic app and it worked fine.

Thank you Cebe. After digging further I found my URLRule helpers was causing the problem.
My App encrypt all the params id,page,sort. Param q was removing.

All good now. Thank you again.

Was this page helpful?
0 / 5 - 0 ratings