app\code\core\Mage\Page\Block\Html\Pager.php
我使用我的消息插件作为例子,我是参考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.toolbar')->setCollection($this->_getCollection());
/* messages.toolbar 是随便写的*/
$this->setChild('toolbar', $toolbar);
return parent::_prepareLayout();
}
/*protected function _prepareLayout(){ //查询条件
parent::_prepareLayout();
$brand_id= $this->getRequest()->getParam('id');
$collection = Mage::getModel('catalog/product')->getCollection();
if (is_numeric($brand_id)) {
$collection->addFieldToFilter('brand', array('eq' => $brand_id));
}
$pager = $this->getLayout()->createBlock('page/html_pager')
->setTemplate('page/html/test.pager.phtml')
->setLimit($this->page_size)
->setCollection($collection);
$this->setChild('pager', $pager);
return $this;
}*/
protected function _getCollection()
{
return $this->_collection;
}
public function getCollection()
{
return $this->_getCollection();
}
在我们的模板文件phtml中
<?php echo $this->getToolbarHtml(); ?>
<div id="_mcePaste"><!-- table --></div>
<div id="_mcePaste"><?php if($this->count()): /*判断是否为空*/?></div>
<div id="_mcePaste"><?php echo $this->getToolbarHtml(); /*获得分页工具条*/?></div>
<div id="_mcePaste"><table id="my-messages-table"></div>
<div id="_mcePaste"><col width="12%" /></div>
<div id="_mcePaste"><col width="68%" /></div>
<div id="_mcePaste"><col width="12%" /></div>
<div id="_mcePaste"><col width="8%" /></div>
<div id="_mcePaste"><thead></div>
<div id="_mcePaste"><tr></div>
<div id="_mcePaste"><th><?php echo $this->__('Date(m/d/y)') ?></th></div>
<div id="_mcePaste"><th><?php echo $this->__('Messages') ?></th></div>
<div id="_mcePaste"><th><?php echo $this->__('Download') ?></th></div>
<div id="_mcePaste"><th><?php echo $this->__('Link') ?></th></div>
<div id="_mcePaste"></tr></div>
<div id="_mcePaste"></thead></div>
<div id="_mcePaste"><tbody></div>
<div id="_mcePaste"><?php foreach ($this->getCollection() as $i=>$message): /* 这里是最重要的地方,一定要用$this->getCollection()来获得Collection */ ?></div>