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);相对应
        }  
时间: 2025-01-07 15:36:48

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

Rails 列表加分页功能

rails 的默认列表是取的所有数据,如果数据量较大,这种方式肯定有问题: 第一,会导致内存超出; 第二,页面会特别长; 第三,加载也较慢. 这个时候,分页需求就来了,总不能自己写分页吧,这也太麻烦了. github.com上搜索一下,哦买噶,居然已经有这gem包,果断拿来用. 用法很简单,只需要在Gemfile里面引入"kaminari"就行了,代码如下: gem 'kaminari' 使用如下: controller: @users = User.order(:name).page

Magento给新产品页面添加分页

本文介绍如何让magento创建一个带分页功能的新到产品页面,方便我们在首页或者其它CMS Page调用和展示新到产品. 在Magento我们经常有的做法是建立一个可以调用新产品的block,然后通过: {{block type="catalog/product_new" column_count="6" products_count="100" name="home.catalog.product.new" alias=&q

drf 其他功能组件 - 限流-过滤-排序-分页-异常处理-生成接口文档-Xadmin

目录 限流Throttling 使用 可选限流类 实例 过滤Filtering 排序 分页Pagination 可选分页器 异常处理 Exceptions REST framework定义的异常 自动生成接口文档 安装依赖 设置接口文档访问路径 文档描述说明的定义位置 访问接口文档网页 Xadmin 安装 使用 限流Throttling 可以对接口访问的频次进行限制,以减轻服务器压力. 一般用于付费购买次数,投票等场景使用. 使用 可以在配置文件中,使用DEFAULT_THROTTLE_CLAS

如何在App中实现IM功能之七快速实现聊天列表排序模块——箭扣科技Arrownock

如何在App中实现IM功能 之七 快速实现聊天列表排序模块 一台设备由于用户的使用习惯差异,可能导致设备的时间和实际收发消息的时间不同,这里与大家分享利用anIM收发消息时,如何根据数据中的msgId和timestamp来进行排序. 实现逻辑 发消息时,我们需要调用anIM的sendMessage方法(同类方法还有sendBinary, sendMessageToTopic等等),这个方法有一个返回数据为messageId,即当前这一条聊天消息的唯一标识,我们需要将这个messageId保存下来

MVC5 Entity Framework学习之添加排序、筛选和分页功能

前一篇文章中实现了对Student 实体的的基本CRUD操作,在这篇文章中将演示如何为Students Index页面添加排序.筛选和分页的功能. 下面是当完成排序.筛选和分页功能后的截图,你可以点击列标题来进行排序. 1.为 Students Index页面添加列排序链接 要为Students Index页面添加排序功能,你需要修改Student controller的Index方法,并为Student Index视图添加代码. 向Index方法添加排序功能 打开Controllers\Stu

MVC4中的列表排序、过滤、分页

在上一个课程中,我们已经学习了如何使用 EF 对 Student 实体进行增.删.改.查处理.这次的课程我们将对学生的 Index 页面加入排序.过滤以及分页的功能.还要创建一个页面完成简单的分组. 下面的截图展示了完成之后的页面,列的标题作为链接支持用户通过点击完成排序,点击标题可以在升序和降序之间进行切换. 3-1  在 Students 的 Index 页面增加列标题链接 为 Index 页面增加排序的功能,我们需要修改 Student 控制器的 Index 方法,还需要为 Student

CRM-展示列表,分页功能

目录 一.admin (创建超级用户) 二.展示列表 三.分页(封装成类) 一.admin (创建超级用户) 1.注册: 1.创建一个超级管理员,使用如下命令: python manage.py createsuperuser 2.输入打算使用的登录名: username:michaelshu 3.输入email: Email address: 4.输入密码,需要输入两次,输入过程中密码不显示: Password: (既要有数字又要有字母) Password (again): 当两次密码都相同的

数据库中复杂的联查+筛选条件+排序+分页

数据库中复杂的联查+筛选条件+排序+分页一直是比较头疼的问题 为了模拟这个问题,首先建立两个表 create table t_userType ( id int identity(1,1) not null primary key, name varchar(50) ) GO create table t_user ( id int identity(1,1) not null primary key, t_userTypeId int not null, name varchar(50), f

Asp.net MVC 3实例学习之ExtShop(四)——完成产品列表页

在完成产品列表页前要做一些准备功夫.首先是去下载MvcPager用了为产品列表分页.下载的可能是基于MVC 2的,没关系,可以用在MVC 3上.如果有担心,下载源代码重新编译一次好了.下载后将DLL添加到引用里. 接着是要修改一下路由以实现"Catalog/List/[id]/[page]"的访问.打开"Global.asax.cs"文件,然后在默认路由之前添加以下代码: 1                          routes . MapRoute (