How to use pagination in Magento

classYour_Module_Block_Entityname_ListextendsMage_Core_Block_Template
{    protected  function _construct(){
     // We get our collection through our model
        parent::_construct();
      // Instantiate a new Pager block
        $this->_entities =Mage::getModel('your_module/entityname')->getCollection()->setOrder('created_at', 'desc');
      // /!\ The limit must be set before the collection
        $pager =newMage_Page_Block_Html_Pager();// We set our limit (here an integer store in configuration).
      // Add our Pager block to our current list block 
        $pager
            ->setLimit((int)Mage::getStoreConfig('your_module/entityname/pagination'))->setCollection($this->_entities);
        $this->setChild('pager', $pager);
    }
}

 

You 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');?>

 

 

 

posted on 2014-04-12 17:13  冯亮  阅读(213)  评论(0编辑  收藏  举报

导航