magento 分页

简介:
Java代码   收藏代码
  1. app\code\core\Mage\Page\Block\Html\Pager.php  

我使用我的消息插件作为例子,我是参考magento的tag wishlist order如何添加pager代码的。你直接套用本教程中的代码,基本就可以实现给你的插件添加分页的功能了。

 在插件block的php文件中

Java代码   收藏代码
  1. <?php  
  2. class Hellokeykey_Messagesbox_Block_Messagesbox extends Mage_Core_Block_Template  
  3. {  
  4.     public function __construct()  
  5.     {  
  6.         parent::__construct();  
  7.         /*声明我的collection*/  
  8.         $this->_collection = Mage::getModel('messagesbox/messagesbox')->getCollection();  
  9.         /*对我的collection进行筛选,我将结果按照建立时间进行了逆向排序,所以最近添加的会显示在前面。并且只显示激活状态的消息*/  
  10.         $this->_collection->setOrder('created_time''DESC')->addFilter('status',array('status' => '1'));  
  11.     }  
  12.     public function count()  
  13.     { /* 判断是否为空,在我们的phtml输出前判断下,为空的话说出一段html作为提示 */  
  14.         return $this->_collection->getSize();  
  15.     }  
  16.   
  17.     public function getToolbarHtml()  
  18.     { /* 获得toobar,在phtml中用到 */  
  19.         return $this->getChildHtml('toolbar');  
  20.     }  
  21.   
  22.     protected function _prepareLayout()  
  23.     { /* 定义我们的toobar */  
  24.         $toolbar = $this->getLayout()->createBlock('page/html_pager''messages.toolbar')->setCollection($this->_getCollection());  
  25.         /* messages.toolbar 是随便写的*/  
  26.           
  27.         $this->setChild('toolbar', $toolbar);  
  28.         return parent::_prepareLayout();  
  29.     }  
  30.     /*protected function _prepareLayout(){ //查询条件 
  31.         parent::_prepareLayout(); 
  32.         $brand_id= $this->getRequest()->getParam('id'); 
  33.         $collection = Mage::getModel('catalog/product')->getCollection(); 
  34.         if (is_numeric($brand_id)) { 
  35.              $collection->addFieldToFilter('brand', array('eq' => $brand_id));             
  36.         } 
  37.         $pager = $this->getLayout()->createBlock('page/html_pager') 
  38.                 ->setTemplate('page/html/test.pager.phtml') 
  39.                 ->setLimit($this->page_size) 
  40.             ->setCollection($collection); 
  41.         $this->setChild('pager', $pager); 
  42.         return $this;          
  43.     }*/  
  44.     protected function _getCollection()  
  45.     {  
  46.         return $this->_collection;  
  47.     }  
  48.   
  49.     public function getCollection()  
  50.     {  
  51.         return $this->_getCollection();  
  52.     }  

 在我们的模板文件phtml中

Java代码   收藏代码
  1. <?php echo $this->getToolbarHtml(); ?>  
 
Java代码   收藏代码
  1. <div id="_mcePaste"><!-- table --></div>  
  2. <div id="_mcePaste"><?php if($this->count()): /*判断是否为空*/?></div>  
  3. <div id="_mcePaste"><?php echo $this->getToolbarHtml(); /*获得分页工具条*/?></div>  
  4. <div id="_mcePaste"><table id="my-messages-table"></div>  
  5. <div id="_mcePaste"><col width="12%" /></div>  
  6. <div id="_mcePaste"><col width="68%" /></div>  
  7. <div id="_mcePaste"><col width="12%" /></div>  
  8. <div id="_mcePaste"><col width="8%" /></div>  
  9. <div id="_mcePaste"><thead></div>  
  10. <div id="_mcePaste"><tr></div>  
  11. <div id="_mcePaste"><th><?php echo $this->__('Date(m/d/y)') ?></th></div>  
  12. <div id="_mcePaste"><th><?php echo $this->__('Messages') ?></th></div>  
  13. <div id="_mcePaste"><th><?php echo $this->__('Download') ?></th></div>  
  14. <div id="_mcePaste"><th><?php echo $this->__('Link') ?></th></div>  
  15. <div id="_mcePaste"></tr></div>  
  16. <div id="_mcePaste"></thead></div>  
  17. <div id="_mcePaste"><tbody></div>  
  18. <div id="_mcePaste"><?php foreach ($this->getCollection() as $i=>$message): /* 这里是最重要的地方,一定要用$this->getCollection()来获得Collection */ ?></div>  
相关文章
|
2月前
|
PHP
thinkphp修改分页配置文件
thinkphp修改分页配置文件
19 0
|
7月前
|
SQL 前端开发
JavaWeb12(实现基础分页&模糊查询的分页)
JavaWeb12(实现基础分页&模糊查询的分页)
|
PHP
PHP:laravel自定义分页page查询方法
PHP:laravel自定义分页page查询方法
89 0
ThinkPHP5.0分页查询测试
ThinkPHP5.0分页查询测试
|
前端开发 PHP
Laravel8 分页样式问题
一、问题:分页样式只显示上一页和下一页,中间的页码不显示
|
前端开发 PHP
|
JavaScript 前端开发 PHP