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">
<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 产品列表排序、分页功能的更多相关文章
- Jquery、Ajax实现新闻列表页分页功能
前端页面官网的开发,离不开新闻列表,新闻列表一般都会有分页的功能,下面是我自己总结加查找网上资料写的一个分页的功能,记录一下. 首先,官网的开发建立在前后端分离的基础上: 再有,后端小伙伴们提供列表页 ...
- Magento给新产品页面添加分页
本文介绍如何让magento创建一个带分页功能的新到产品页面,方便我们在首页或者其它CMS Page调用和展示新到产品. 在Magento我们经常有的做法是建立一个可以调用新产品的block,然后通过 ...
- sharepoint 2010 列表数据分页控件介绍 pagination UserControl
转:http://blog.csdn.net/chenxinxian/article/details/8714391 这里主要是介绍下最近开发的一个sharepoint列表或者文档库的分页控件,并且把 ...
- day74:drf:drf其他功能:认证/权限/限流/过滤/排序/分页/异常处理&自动生成接口文档
目录 1.django-admin 2.认证:Authentication 3.权限:Permissions 4.限流:Throttling 5.过滤:Filtering 6.排序:OrderingF ...
- 一行代码调用实现带字段选取+条件判断+排序+分页功能的增强ORM框架
问题:3行代码 PDF.NET是一个开源的数据开发框架,它的特点是简单.轻量.快速,易上手,而且是一个注释完善的国产开发框架,受到不少朋友的欢迎,也在我们公司的项目中多次使用.但是,PDF.NET比起 ...
- jPList – 实现灵活排序和分页功能的 jQuery 插件
jPList 是一个灵活的 jQuery 插件,可以用于任何 HTML 结构的排序,分页和筛选.它支持的数据源包括:PHP + MySQL,ASP.NET + SQL Server,PHP + SQL ...
- CRM-展示列表,分页功能
目录 一.admin (创建超级用户) 二.展示列表 三.分页(封装成类) 一.admin (创建超级用户) 1.注册: 1.创建一个超级管理员,使用如下命令: python manage.py ...
- 测试开发【提测平台】分享9-DBUntils优化数据连接&实现应用搜索和分页功能
微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 从本期开始知识点讲以思维导图的形式给出,内容点会按照讲解-应用-展示的形式体现,这样会更清晰些. DBUntils连接池 在项目中链接数据 ...
- 简单封装分页功能pageView.js
分页是一个很简单,通用的功能.作为一个有经验的前端开发人员,有义务把代码中类似这样公共的基础性的东西抽象出来,一来是改善代码的整体质量,更重要的是为了将来做类似的功能或者类似的项目,能减少不必要的重复 ...
随机推荐
- 转自 z55250825 的几篇关于FFT的博文(一)
关于FFT,咱们都会迫不及待地 @ .....(大雾)(貌似被玩坏了...) .....0.0学习FFT前先orz FFT君. 首先先是更详细的链接(手写版题解点赞0v ...
- BIOS与UEFI、MBR和GPT介绍
操作步骤: UEFI是取代传统BIOS的,全称“统一的可扩展固件接口”.MBR则是传统的分区表类型,最大的缺点则是不支持容量大于2T的硬盘.GPT则弥补了MBR这个缺点,最大支持18EB的硬盘,是基于 ...
- Sharepoint 2010 用VS定制Master,并且每个Web应用同一个Master
转:http://***/html/blogs/20130407/1381.htm 最近做了一个项目管理系统,要求用Sharepoint,有个特别的功能就是通过创建出来的子站点要求应用同一个Maste ...
- 解决 android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
错误日志1: 06-13 10:55:50.410: E/KVLog(1129): Error info:java.lang.ClassCastException: android.widget.Li ...
- C# VS2010中,用微软自带的System.Data.OracleClient来连接Oracle数据库
由于微软在.Net框架4.0中已经决定撤销使用System.Data.OracleClient,造成在VS2010中无法连接Oracle数据库,但它还依旧存在于.Net架构中,我们可以通过自己引用 C ...
- iLearning D3.js 2.0 released
There are some great changes in 2.0 version. Console in tutorial: In previous version, there will be ...
- 【原创】Java移位运算
学习移位运算,首先得知道参与移位运算的类型的位数,那先来复习下Java基础类型的占位数吧. Java基础类型 Java基础类型总结一览表 类型 二进制位数 最大值 最小值 初始化值 表示形式 带符号 ...
- POJ2104&&HDU2665(静态区间第K小)
题目大意 给定一个有N个数字的序列,然后又m个查询,形式如下: l r k 要求你返回区间[l,r]第k小的数是哪个 题解 终于弄懂主席树是个啥东西了,O(∩_∩)O~~,这题正是主席树的裸题,主席树 ...
- 问题-[DelphiXE7]新建的安桌模拟器运行程序闪退
问题现象:在DelphiXE7中的手机模拟器,每次运行程序,就闪退?问题处理:在 DelphiXE7的目录中,有一个AVD文件夹,删除后就好了.
- Sublime Text 教程
编辑器的选择(Editor Choices) 从初学编程到现在,我用过的编辑器有EditPlus.UltraEdit.Notepad++.Vim.TextMate和Sublime Text,如果让我从 ...