我们以 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);相对应
}

magento 产品列表排序、分页功能的更多相关文章

  1. Jquery、Ajax实现新闻列表页分页功能

    前端页面官网的开发,离不开新闻列表,新闻列表一般都会有分页的功能,下面是我自己总结加查找网上资料写的一个分页的功能,记录一下. 首先,官网的开发建立在前后端分离的基础上: 再有,后端小伙伴们提供列表页 ...

  2. Magento给新产品页面添加分页

    本文介绍如何让magento创建一个带分页功能的新到产品页面,方便我们在首页或者其它CMS Page调用和展示新到产品. 在Magento我们经常有的做法是建立一个可以调用新产品的block,然后通过 ...

  3. sharepoint 2010 列表数据分页控件介绍 pagination UserControl

    转:http://blog.csdn.net/chenxinxian/article/details/8714391 这里主要是介绍下最近开发的一个sharepoint列表或者文档库的分页控件,并且把 ...

  4. day74:drf:drf其他功能:认证/权限/限流/过滤/排序/分页/异常处理&自动生成接口文档

    目录 1.django-admin 2.认证:Authentication 3.权限:Permissions 4.限流:Throttling 5.过滤:Filtering 6.排序:OrderingF ...

  5. 一行代码调用实现带字段选取+条件判断+排序+分页功能的增强ORM框架

    问题:3行代码 PDF.NET是一个开源的数据开发框架,它的特点是简单.轻量.快速,易上手,而且是一个注释完善的国产开发框架,受到不少朋友的欢迎,也在我们公司的项目中多次使用.但是,PDF.NET比起 ...

  6. jPList – 实现灵活排序和分页功能的 jQuery 插件

    jPList 是一个灵活的 jQuery 插件,可以用于任何 HTML 结构的排序,分页和筛选.它支持的数据源包括:PHP + MySQL,ASP.NET + SQL Server,PHP + SQL ...

  7. CRM-展示列表,分页功能

    目录 一.admin (创建超级用户) 二.展示列表 三.分页(封装成类)   一.admin (创建超级用户) 1.注册: 1.创建一个超级管理员,使用如下命令: python manage.py ...

  8. 测试开发【提测平台】分享9-DBUntils优化数据连接&实现应用搜索和分页功能

    微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 从本期开始知识点讲以思维导图的形式给出,内容点会按照讲解-应用-展示的形式体现,这样会更清晰些. DBUntils连接池 在项目中链接数据 ...

  9. 简单封装分页功能pageView.js

    分页是一个很简单,通用的功能.作为一个有经验的前端开发人员,有义务把代码中类似这样公共的基础性的东西抽象出来,一来是改善代码的整体质量,更重要的是为了将来做类似的功能或者类似的项目,能减少不必要的重复 ...

随机推荐

  1. msvc2010生成的指令序列有问题,可能跟pgo有关

    正常序列 有问题序列 这段代码程序启动是执行,会导致崩溃 工程使用ltcg pgo,也就是说,第一次编译连接完成后,会跑一次profile,再执行连接器代码生成优化. 构建记录显示,ltcg已跑完,说 ...

  2. 安装 Homebrew

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ...

  3. 【Android】Android程序保护与破解浅析

    此文源自组内成员分享的PPT,其他成员的文档由于没有得到授权,暂不公开. 本文命令如果没有特殊注明,均为windows 7环境. 本文只涉及大概的知识点,不涉及具体的细节,需要注意. 反编译 apkt ...

  4. Cookie的Domain

    每个Cookie都有常用的几个元素:name.value.expires.domain Cookie的Domain 设置cookies时,可以设置cookie的域名参数domain,标识cookie在 ...

  5. Oracle.ManagedDataAccessDTC.dll 使用

    ODP.NET, Managed Driver Setup This section explains the setup and configuration steps required for u ...

  6. Git 远程分支的查看及相关问题

    命令:git ls-remote -t 或者 git ls-remote --tag 运行结果如下: 0975ebc0f9a6b42ecbe066a50a26a678a0753b4d refs/tag ...

  7. shell常识总结

    #!/bin/bash cmd="ls -lt | grep ^d | awk 'NR==1 {print $9}'" $cmd 这是一个获取文件夹名字的脚本,但是却提示: ls: ...

  8. Android webView 中loadData方法加载 带中文时出现乱码

    WebView出现乱码用LoadData方法来解析html的,但是据说这是官方的一个BUG,不能用来解析中文. 采用loadDataWithBaseURL的方法,其中codeingType设置为utf ...

  9. HTTP 和 SOAP 标头 来传递用户名密码 验证webservice用户认证

    支持自定义的 HTTP 和 SOAP 标头 注意:本主题中的内容适用于 Microsoft Office SharePoint Server 2007 SP1. 对于 Web 服务,您可以使用 HTT ...

  10. Redis+Spring缓存实例(windows环境,附实例源码及详解)

    原文出处: 小宝鸽 一.Redis了解 1.1.Redis介绍: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串). ...