magento 分页

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>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值