我们以 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. java 页面url传值中文乱码的解决方法

    parent.window.location.href 和 iframe中src的乱码问题.要在这两个url地址中传中文,必须加编码,然后再解码.编码:encodeURI(encodeURI(&quo ...

  2. 致改变——总结&规划(2016·一)

    今天是立夏,过完这一天意味着农历2016年的第一季度已经过去了,也意味着真正的夏天已经来了.如果说春天是作物的播种期的话,那夏天可以看做是作物的成长期,也是农民伯伯们最繁忙的时期.本文主要对自己过去的 ...

  3. C# 多线程(lock,Monitor,Mutex,同步事件和等待句柄)

    本篇从 Monitor,Mutex,ManualResetEvent,AutoResetEvent,WaitHandler 的类关系图开始,希望通过本篇的介绍能对常见的线程同步方法有一个整体的认识,而 ...

  4. How to get FlowLayoutPanel.AutoSize to work with FlowBreak

    have a problem with a FlowLayoutPanel and I don't know how to solve it. I'm placing two FlowLayoutPa ...

  5. POJ 1185 (状态压缩DP)

    中文题目,题意就不说了. 不得不说这是一道十分经典的状态压缩DP的题目. 思路: 通过分析可以发现,第i行的格子能不能放大炮仅与第i-1和i-2行的放法有关,而与前面的放法无关,因此,如果我们知道了i ...

  6. poj2192(搜索)

    这个题目对于两个字符串A,B是否可以通过规则生成C. import java.util.Scanner; public class Main { public static void main(Str ...

  7. HW2.5

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  8. Codeforces Round #226 (Div. 2)A. Bear and Raspberry

    /* 贪心的找到相邻两项差的最大值,再减去c,结果若是负数答案为0. */ 1 #include <stdio.h> #define maxn 105 int num[maxn]; int ...

  9. Google正确搜索方法

    以下是目前所有的Google搜索命令语法,它不同于Google的帮助文档,因为这里介绍了几个Google不推荐使用的命令语法.大多数的Google搜索命令语法有它特有的使用格式,希望大家能正确使用.我 ...

  10. webpack文档

    https://github.com/liunian/webpack-doc/blob/master/SUMMARY.md