magento产品列表排序、分页功能原理

我们以 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">
                <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>
                    <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>
                    <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
                </block>
            </block>
        </reference>
    </catalog_category_layered>

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

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

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

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

protected function _beforeToHtml()
    {
        $toolbar = $this->getToolbarBlock();//实例化ToolbarBlock

        // called prepare sortable parameters
        $collection = $this->_getProductCollection();//获取产品集合
        // 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);
        }
        
        // set collection to toolbar and apply sort
        $toolbar->setCollection($collection);//用ToolbarBlock实例对产品集合排序和分页


        $this->setChild('toolbar', $toolbar);//设置ToolbarBlock实例为当前Block的Child,在显示的时候会有用

        Mage::dispatchEvent('catalog_block_product_list_collection', array(
            'collection' => $this->_getProductCollection()
        ));

        $this->_getProductCollection()->load();

        return parent::_beforeToHtml();
    }


$toolbar->setCollection($collection);
是如何实现排序和分页功能呢,来看这个方法就知道了,打开Mage_Catalog_Block_Product_List_Toolbar类

public function setCollection($collection)
    {  
        $this->_collection = $collection;//对象引用

        $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);
        }//分页功能
        if ($this->getCurrentOrder()) {
            $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        }//排序功能
        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);相对应
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值