Yii Framework 2 : Custom Pagination

When we are having more data and have to split it as many pages using pagination. By default, Yii2.0 is having that option for pagination. For the pagination, we have to send some information to page they are total item count, page size(number of records per page), current page etc.

Simple Pagination

SampleController.php

<?php
//............

class CategoryController extends Controller
{
    //...........
    function actionIndex()
    {
        $query = User::find()->where(['status' => 1]);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count()]);
        $models = $query->offset($pages->offset)->limit($pages->limit)->all();

        return $this->render('index', ['models' => $models,'pages' => $pages]);
    }
}
//..........
?>
index.php

<?php
foreach ($models as $model) {
    // display $model here
}
// display pagination
echo LinkPager::widget(['pagination' => $pages]);
?>

CustomPagination.php Class

LinkPager is handling the pagination link with properties. We can customize the link properties using the below method or class. Just i created 'CustomPagination' class in "project/components/" folder and set namespace as "app\components". We removed the link and added onclick attribute in every pagination link.

<?php
namespace app\components;

use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\base\Widget;
use yii\data\Pagination;

class CustomPagination extends \yii\widgets\LinkPager
{
    public function init()
    {
        parent::init();
    }

    public function run()
    {
        parent::run();
    }

    protected function renderPageButton($label, $page, $class, $disabled, $active)
    {
        $options = ['class' => $class === '' ? null : $class];
        if ($active) {
            Html::addCssClass($options, $this->activePageCssClass);
        }
        if ($disabled) {
            Html::addCssClass($options, $this->disabledPageCssClass);
            return Html::tag('li', Html::tag('span', $label), $options);
        }
        $linkOptions              = $this->linkOptions;
        $linkOptions['data-page'] = $page;
        $linkOptions['onclick']   = 'submit_form(' . $page . ')';

        return Html::tag('li', Html::a($label, '#pagination', $linkOptions), $options);
    }
}
?>

Custom Pagination For Gridview

Now We removed the basic link pager and call the custom pagination class.
Controller.php

<?php
//............

class CategoryController extends Controller
{
    public function index()
    {
        $query      = Tablereport::find();
        $countQuery = clone $query;

        $pages = new Pagination(['totalCount' => $countQuery->count()]);

        $dataProvider = new ActiveDataProvider(['query' => $query]);
        return $this->render('GridView', ['dataProvider' => $dataProvider, 'pages' => $pages]);
    }
}
//..........
?>
GridView.php
Using 'layout' properties, we can remove the basic link pager.

<?php
use yii\helpers\Html;
use yii\grid\GridView;
use app\components\CustomPagination;
?>
<div class="report-index">
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,
        'layout' => "{summary}\n{items}",
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',
            'report_name',
            'report_table',
            'report_query:ntext',
            'created_on',
            // 'created_by',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
    <div id="custom-pagination">
        <?php
        echo CustomPagination::widget([
            'pagination' => $pages,
        ]);
        ?>
    </div>
</div>

  • Ksenia Yarovaya

    thanks a lot!

  • vishal

    in this `linkOptions` where is submit_form() function ??

    $linkOptions[‘onclick’]=’submit_form(‘.$page.’)’;