我使用我的消息插件作为例子,我是参考magento的tag wishlist order如何添加pager代码的。你直接套用本教程中的代码,基本就可以实现给你的插件添加分页的功能了。
在插件block的php文件中
<?php class Hellokeykey_Messagesbox_Block_Messagesbox extends Mage_Core_Block_Template { public function __construct() { parent::__construct(); /*声明我的collection*/ $this->_collection = Mage::getModel('messagesbox/messagesbox')->getCollection(); /*对我的collection进行筛选,我将结果按照建立时间进行了逆向排序,所以最近添加的会显示在前面。并且只显示激活状态的消息*/ $this->_collection ->setOrder('created_time', 'DESC') ->addFilter('status',array('status' => '1')); } public function count() { /* 判断是否为空,在我们的phtml输出前判断下,为空的话说出一段html作为提示 */ return $this->_collection->getSize(); } public function getToolbarHtml() { /* 获得toobar,在phtml中用到 */ return $this->getChildHtml('toolbar'); } protected function _prepareLayout() { /* 定义我们的toobar */ $toolbar = $this->getLayout()->createBlock('page/html_pager', 'messages_messages.toolbar')/* messages_messages.toolbar 是随便写的*/ ->setCollection($this->_getCollection()); $this->setChild('toolbar', $toolbar); return parent::_prepareLayout(); } protected function _getCollection() { return $this->_collection; } public function getCollection() { return $this->_getCollection(); }
在我们的模板文件phtml中