Gridview Custom Pagination

Hi,

I want custom pagination on CGridview. I want pagination dropdown on index.php view so that whenever I change value from dropdown, then grid pagination change

Index.php




<div class="country-index">

<select><option value="10">10</option>

<option value="20">20</option>

<option value="30">30</option>

</select>

    <h1><?= Html::encode($this->title) ?></h1>

    <?php //echo $this->render('_search', ['model' => $searchModel]); ?>


    <p>

        <?= Html::a('Create Country', ['create'], ['class' => 'btn btn-success']) ?>

    </p>


    <?= GridView::widget([

        'caption'=>'<h2>Javed</h2>',

    		'showHeader'=>true,

    		'layout' => "{summary}\n{items}\n{pager}",

    		'options' => array('class' => 'grid-view'),

    		'tableOptions' => array('class' => 'table table-striped table-bordered'),

//     		'pager'=>'yii\widgets\LinkSorter',

    		'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],

            'code',

            'name',

            'population',


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>


</div>



CountrySearch.php




$dataProvider = new ActiveDataProvider([

            'query' => $query,

        		'pagination' => [

        			//'pageSize' => 2,

        			'pageSize'=> isset(Yii::$app->params['defaultPageSize']) ? Yii::$app->params['defaultPageSize'] : 5,

        		],




        ]);



Then you have to create your own pager widget class (either standalone or extending \yii\widgets\LinkPager) and configure GridView like:




<?= GridView::widget([

        ...

        'pager' => [

            'class' => 'my\custom\Pager',

            //other pager config if nesessary

        ],

        ...



LinkSorter is not suitable for pagination, it is used in the same way as sort links in GridView table header.

I have made following changes, but I still can’t find filter widget:




<?= GridView::widget([

        

    		'showHeader'=>true,

    		'layout' => "{summary}\n{items}\n{pager}",

    		'options' => array('class' => 'grid-view'),

    		'tableOptions' => array('class' => 'table table-striped table-bordered'),

//     		'pager'=>'yii\widgets\LinkSorter',

    		'dataProvider' => $dataProvider,

       		 'filterModel' => $searchModel,

    		'pager' => [

    			'class' => '\yii\widgets\LinkPager',

    		//other pager config if nesessary

    		]...




?

You’ve told you need pager widget, not filter.

As for dropdown pager - I made quick draft of it, you may try it:

Code here

Put it in frontend/widgets directory - if you use advanced app.

In Gridview:




        ...

        'pager'        => [

            'class' => 'frontend\widgets\DropdownPager',

        ],

        ...



If your app structure differs from advanced app - choose destination folder and change namespace according to your structure.

Thanks for support. Well I am using basic APP not advance. So in basic app where I should add DropdownPager.php file.

Even I have created following directory but it still I get error (Class ‘frontend\widgets\DropdownPager’ not found)

frontend\widgets\

Well, you can create for example widgets folder and change namespace in file:


namespace app\widgets;

and in GridView respectively:




        ...

        'pager'        => [

            'class' => 'app\widgets\DropdownPager',

        ],

        ...



Modified a bit current page display (line 29 and line 69)

I still get this error:

Unable to find ‘app\widgets\DropdownPager’ in file: /home/javed/workspace/mfc/widgets/DropdownPager.php. Namespace missing?

MODEL:CountrySearch.php




<?php


namespace app\models;

namespace app\widgets;




use Yii;

use yii\base\Model;

use yii\data\ActiveDataProvider;

use app\models\Country;


/**

 * CountrySearch represents the model behind the search form about `app\models\Country`.

 */

class CountrySearch extends Country

{

    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['code', 'name'], 'safe'],

            [['population'], 'integer'],

        ];

    }


    /**

     * @inheritdoc

     */

    public function scenarios()

    {

        // bypass scenarios() implementation in the parent class

        return Model::scenarios();

    }


    /**

     * Creates data provider instance with search query applied

     *

     * @param array $params

     *

     * @return ActiveDataProvider

     */

    public function search($params)

    {

        $query = Country::find();


        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        	'pagination' => [	'pageSize'=> isset(Yii::$app->params['defaultPageSize']) ? Yii::$app->params['defaultPageSize'] : 5,],


        ]);




        if (!($this->load($params) && $this->validate())) {

            return $dataProvider;

        }


        $query->andFilterWhere([

            'population' => $this->population,

        ]);


        $query->andFilterWhere(['like', 'code', $this->code])

            ->andFilterWhere(['like', 'name', $this->name]);


        return $dataProvider;

    }

}




Index.php




use yii\helpers\Html;

use yii\grid\GridView;

use yii\bootstrap\Nav;

use yii\bootstrap\NavBar;

use yii\widgets\LinkPager;


...

<?= LinkPager::widget(['pagination'=>$dataProvider->pagination]);?>

    <?= GridView::widget([

    		'tableOptions' => [

    		'id'=>'purcd'

    		],

        	'caption'=>'<h2>Javed</h2>',

    		'showHeader'=>true,

    		'layout' => "{summary}\n{items}\n{pager}",

    		'options' => array('class' => 'grid-view'),

    		'tableOptions' => array('class' => 'table table-striped table-bordered'),

//     		'pager'=>'yii\widgets\LinkSorter',

    		'dataProvider' => $dataProvider,

       		 'filterModel' => $searchModel,

    		'pager' => [

//     			'firstPageLabel' => 'First Page',

//     			'lastPageLabel' => 'Last Page',

//     			'class' => '\yii\widgets\LinkPager',

    			'class' => 'app\widgets\DropdownPager',

    		//other pager config if nesessary

    		],

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],

            'code',

            'name',

            'population',


            //['class' => 'yii\grid\ActionColumn'],

    		['class' => 'yii\grid\ActionColumn',

    		'template' => '{view}{update}{delete}{javi_button}',

    		'buttons' => ['javi_button' => function ($url, $model) {

    				return Html::a('<span class="glyphicon glyphicon-pushpin"></span>', $url, []);

    			}],

    		],

        ],

    ]);


...



Why did you put in your model CountrySearch.php second namespace:




...

namespace app\models;

namespace app\widgets;

...



???

Leave one namespace in model




...

namespace app\models;

...



Modify namespace in widget (file DropdownPager.php):

instead of




...

namespace frontend\widgets;

...



you should put




...

namespace app\widgets;

...



Why did you put in your model CountrySearch.php second namespace:




...

namespace app\models;

namespace app\widgets;

...



???

Leave one namespace in model




...

namespace app\models;

...



Modify namespace in widget (file DropdownPager.php):

instead of




...

namespace frontend\widgets;

...



you should put




...

namespace app\widgets;

...



[/quote]

Now i can see per page records link at the end of gridview but I can’t change.

6027

Gridview.png

It’s working for me.

Check if there are javascript errors on the page. Check the order in which scripts are loading.

Ok I will check. Thanks for helping sergeD. Really appreciate