2

I have a number of collections which have blocks that output the contents to frontend routes (My Account, frontend features).

I have need to paginate some of these. I know that pagination methods exist on collections but is there a way to handle the querystring portion as well? I hate to reinvent the wheel...

2 Answers 2

13

It's actually quite simple.

In the main block that calls your collection to render, add the following:

protected function _prepareLayout()
{
    parent::_prepareLayout();

    $pager = $this->getLayout()->createBlock('page/html_pager', 'your.custom.blockname.pager')
        ->setCollection($this->getCollection()); //call your own collection getter here, name it something better than getCollection, please; *or* your call to getResourceModel()
    $this->setChild('pager', $pager);
    return $this;
}

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

By default this will limit your collection to 10-at-a-time! Sweet!!

Then, from your template phtml associated to your collection, add the pager controls by echoing the output of our other method, getPagerHtml:

<?php echo $this->getPagerHtml(); ?>

Source: Experience. And also, the sales/order_history block in Magento.

1
  • 2
    You can set the paging limits to the block: $pager->setAvailableLimit(array(1=>1, 7=>7, 128=>128)).
    – Marius
    Jul 29, 2013 at 20:15
0

I used this inside Block class (It's Mage_Catalog_Block_Navigation):

$pager = new Mage_Page_Block_Html_Pager();
$pager->setLimit(100)->setCollection(...getCollection());
$this->setChild('pager', $pager);

Your Answer

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

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