17

I am working on custom module. How can i use default magento pagination with my custom collection and also set custom limits on that?

8
  • 1
    When i created my custom module in magento2 for pagination & limits i referred these link (mage-world.com/blog/…) and i could get it done.
    – Arjun
    Commented Aug 8, 2016 at 9:48
  • Could you please explain on how to override the Magento Catalog for adding pagination to custom module! the above link i got some idea, i want to override in Catalog module
    – Sushivam
    Commented Aug 18, 2016 at 7:55
  • @SachinS i thin you are talking about toolbar? Commented Aug 18, 2016 at 7:57
  • Yes exactly...I have described what i tried and error here...magento.stackexchange.com/questions/131896/…
    – Sushivam
    Commented Aug 18, 2016 at 7:58
  • @SachinS what you want achieve with toolbar and did you solve this magento.stackexchange.com/questions/131805/… Commented Aug 18, 2016 at 8:15

1 Answer 1

32

get collection for that

public function getNews()
    {
      //get values of current page
        $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
    //get values of current limit
        $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 1;


        $newsCollection = $this->newscollectionFactory->create();
        $newsCollection->addFieldToFilter('is_active',1);
        $newsCollection->setOrder('title','ASC');
        $newsCollection->setPageSize($pageSize);
        $newsCollection->setCurPage($page);
        return $newsCollection;
    }

Add pagination

protected function _prepareLayout()
{
    parent::_prepareLayout();
    $this->pageConfig->getTitle()->set(__('News'));


    if ($this->getNews()) {
        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager',
            'test.news.pager'
        )->setAvailableLimit(array(5=>5,10=>10,15=>15))->setShowPerPage(true)->setCollection(
            $this->getNews()
        );
        $this->setChild('pager', $pager);
        $this->getNews()->load();
    }
    return $this;
}

adding the child block

public function getPagerHtml()
{
    return $this->getChildHtml('pager');
}

in phtml file

    <?php if ($block->getPagerHtml()): ?>
        <div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
    <?php endif ?>

Reference

34
  • 2
    Pssst: either $this->getRequest()->getParam('p') ?: 1 or, better yet, $this->getRequest()->getParam('p', 1).
    – nevvermind
    Commented Aug 8, 2016 at 13:39
  • I get element with id already exists error for whatever I try to name the pager block I'm creating using your code. Commented Dec 13, 2016 at 14:40
  • @LM_Fielding not faced this issue yet but try deleting var/cache and var/generation folder Commented Dec 14, 2016 at 6:08
  • I was trying to add it for a toolbar at the top and at the bottom, so it was exactly correct. Commented Dec 15, 2016 at 9:07
  • 1
    @LM_Fielding you are adding it twice that is causing the problem. Commented Dec 15, 2016 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.