Question

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...

Was it helpful?

Solution

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.

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top