我们以 catalog_category_layered 控制器为例说明

在catalog.xml 找到catalog_category_layered配置段

    <catalog_category_layered translate="label">
<label>Catalog Category (Anchor)</label>
<reference name="left">
<block type="catalog/layer_view" name="catalog.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>
<reference name="content">
<block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
<span style="color:#cc0000;"><block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block></span>
<action method="addColumnCountLayoutDepend"><layout>empty</layout><count>6</count></action>
<action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>5</count></action>
<action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action>
<action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action>
<action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action>
<span style="color:#cc0000;"> <action method="setToolbarBlockName"><name>product_list_toolbar</name></action></span>
<span style="color:#cc0000;"></block></span>
</block>
</reference>
</catalog_category_layered>

其中catalog/product_list是产品显示的block,而catalog/product_list_toolbar是控制产品排序和分页功能的Block,而把这两个block联系起来的关键就是

    <span style="color: rgb(204, 0, 0); "> <action method="setToolbarBlockName"><name>product_list_toolbar</name></action></span>  

下面来看下实现排序、分页功能的步骤

1、根据配置文件实例化catalog/product_list Block并调用setToolbarBlockName方法设置ToolbarBlock的名字

2、在catalog/product_list block类(Mage_Catalog_Block_Product_List)的_beforeToHtml()方法中实例化ToolbarBlock,并对产品做排序和分页

    protected function _beforeToHtml()
{
<span style="color:#000099;"> $toolbar = $this->getToolbarBlock();//实例化ToolbarBlock</span> // called prepare sortable parameters
<span style="color:#000099;"> $collection = $this->_getProductCollection();//获取产品集合
</span> // use sortable parameters
if ($orders = $this->getAvailableOrders()) {
$toolbar->setAvailableOrders($orders);
}
if ($sort = $this->getSortBy()) {
$toolbar->setDefaultOrder($sort);
}
if ($dir = $this->getDefaultDirection()) {
$toolbar->setDefaultDirection($dir);
}
if ($modes = $this->getModes()) {
$toolbar->setModes($modes);
} <span style="color:#000099;">// set collection to toolbar and apply sort
$toolbar->setCollection($collection);//用ToolbarBlock实例对产品集合排序和分页</span> <span style="color:#3333ff;"> $this->setChild('toolbar', $toolbar);//设置ToolbarBlock实例为当前Block的Child,在显示的时候会有用</span> Mage::dispatchEvent('catalog_block_product_list_collection', array(
'collection' => $this->_getProductCollection()
)); $this->_getProductCollection()->load(); return parent::_beforeToHtml();
}
    <span style="color:#000099;">$toolbar->setCollection($collection);</span>  

是如何实现排序和分页功能呢,来看这个方法就知道了,打开Mage_Catalog_Block_Product_List_Toolbar类

    public function setCollection($collection)
{
$this->_collection = $collection;//对象引用 <span style="color:#009900;"> $this->_collection->setCurPage($this->getCurrentPage()); // we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}//分页功能</span>
<span style="color:#3333ff;">if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}//排序功能</span>
return $this;
}

3、显示

在product list 的phtml文件中调用Mage_Catalog_Block_Product_List类的getToolbarHtml()方法

    <?php echo $this->getToolbarHtml() ?>  
    public function getToolbarHtml()
{
return $this->getChildHtml('toolbar');//显示它的Child Block 与_beforeToHtml()中的$this->setChild('toolbar', $toolbar);相对应
}

最新文章

  1. Flappy Bird 源码走读
  2. css3 perspective perspective-origin属性的理解
  3. Quartz2D 编程指南(二)变换、图案、阴影
  4. 博弈SG
  5. NOIP2009靶形数独[DFS 优化]
  6. HDU1224 DP
  7. openMP的一点使用经验【非原创】
  8. ASP.NET MVC 从IHttp到页面输出
  9. dom 拖拽div
  10. EventBus3.0使用总结
  11. Lucene.Net 2.3.1开发介绍 —— 四、搜索(三)
  12. iOS 9 新特性
  13. ajax基础部分
  14. css中的颜色
  15. python笔记---需求文件requirements.txt的创建及使用
  16. 第一节《Git初始化》
  17. 移动端自动化测试-AppiumApi接口详解
  18. Spring Security使用报错 No bean named &#39;springSecurityFilterChain&#39; is defined
  19. Java基础总结(一)
  20. 自学Linux Shell3.2-切换目录命令cd

热门文章

  1. MCI音乐播放
  2. [转] Java之ACM速成
  3. 编译安装nginx并修改版本头信息—参考实例
  4. 使用 ASR 和 Azure Pack 为 IaaS 工作负荷提供托受管 DR
  5. MySQL海量数据查询优化策略
  6. MVC3中Action返回类型ActionResult类型
  7. 关于sharepoint事件接收器中properties.AfterProperties[&quot;&quot;].Tostring()取值的问题。
  8. Hadoop框架下MapReduce中的map个数如何控制
  9. PC-IIS因为端口问题报错的解决方法
  10. [每日一题] 11gOCP 1z0-053 :2013-10-7 the backup of MULT_DATA................................32