I
I
Islam Ibakaev2020-02-17 01:02:38
Yii
Islam Ibakaev, 2020-02-17 01:02:38

How to make such advanced pagination in yii2?

Good day. There is such a pagination (this is the layout of the portal, which needs to be put on yii2).
In the portal itself, when I added pagination, I got the following view
5e49bb1408a56264069150.jpeg

. What the framework gave by default is not quite what you need. The picture below shows the desired result.
5e4aac19bf84b493944684.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-02-17
@devellopah

Very simple. Change the default settings in the DI container of this class.
1. Using DI Container

<?php

declare(strict_types=1);

namespace frontend\bootstrap;

use Yii;
use yii\base\BootstrapInterface;
use yii\di\Container;
use yii\widgets\LinkPager;

/**
 * @author Maxim Vorozhtsov <myks1992@mail.ru>
 */
class Bootstrap implements BootstrapInterface
{
    /**
     * @inheritDoc
     */
    public function bootstrap($app)
    {
        /** @var Container $container */
        $container = Yii::$container;

        $container->set(LinkPager::class, [
            'prevPageLabel' => false,
            'nextPageLabel' => false,
            'maxButtonCount' => 3,
        ]);
    }
}

You can also insist DI in config. I usually configure there, but someone creates such a bootstrap file. Here, choose for yourself. I choose config. Here is an example:
'container' => [
        'singletons' => [
            CheckAccessInterface::class => yii\rbac\DbManager::class,
            IdentityInterface::class => function () {
                return Yii::$app->user->getIdentity();
            },
        ],
    ],

But for understanding, I designed it in bootstrap. To make it work don't forget to put this class in the Bootstrap config section of the file:
'bootstrap' => [
        frontend\bootstrap\Bootstrap::class
    ],

2. With the creation of a class inherited from the base
<?php

declare(strict_types=1);

namespace frontend\widgets;

class LinkPager extends \yii\widgets\LinkPager
{
    public $prevPageLabel = false;
    public $nextPageLabel = false;
    public $maxButtonCount = 3;
}

And use as usual. If you need to replace in the DataProvider, use the DI container. Or replace the class with your own:
Yii::$container->set('yii\widgets\LinkPager',  'frontend\widgets\LinkPager');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question