标签:
我们以 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);相对应 }
标签:
原文地址:http://www.cnblogs.com/zhengyanbin2016/p/5724830.html