Add pagination to a custom entity listing in Magento
July 19, 2014
You have created a new entity type and want to display it as a paginated list ?
First, integrate the MagePageBlockHtmlPager through your block :
class Your_Module_Block_Entityname_ListextendsMage_Core_Block_Template {
protectedfunction _construct() {
parent::_construct();
// We get our collection through our model
$this->_entities = Mage::getModel('your_module/entityname')
->getCollection()
->setOrder('created_at');
// Instantiate a new Pager block
$pager =newMage_Page_Block_Html_Pager();
// We set our limit (here an integer store in configuration).
// /!\ The limit must be set before the collection
$pager->setLimit((int)Mage::getStoreConfig('your_module/entityname/pagination'))
->setCollection($this->_entities);
// Add our Pager block to our current list block
$this->setChild('pager', $pager);
}
}
ou just need now to include the call in your template (phtml) file :
<divclass="your_module_entities">
<?php foreach($this->_entities as $entity):?>
<divclass="entity">
<h2><?php echo $entity->getAttribute1();?></h2>
<p><?php echo $entity->getAttribute2();?></p>
</div>
<?php endforeach;?>
</div>
<?php echo $this->getChildHtml('pager');?>
And you should be good to go !