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'],
],
]);
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.
index.php?r=search&page=2
| Q | A |
| --- | --- |
| Yii version | 2.0.? |
| PHP version | 5.6 |
| Operating system | windows |
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
<?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,
]);
}
}
?>
<?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;
}
}
<?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.
Most helpful comment
this will disable pagination, you have to add the page param too:
or use the default value of
paramswhich will include all GET params by default.