Magento - GRID FILTER FOR COLUMNS WITH COMPLEX VALUES

In previous articles we
told you how to operate with columns in adminhtml grids. Usually the process is quite fast, for example, if you want to add simple column from the database. But sometimes you need to add column with complex value, processed by renderer. You say: “it’s
not a big deal to use renderers…” Right, until you face with sort and filter.. By
default Magento applies grid filter directly to the corresponding column. But renderer might contain data from separate columns. At this point we need to choose right way to make filter and sort work correctly. For example, we are going to add a new column
“Address”
to the orders grid which consists of the values from few columns: city, street, postcode.
First of all, we should join address table to the collection:


1

2

3

4

$collection->joinLeft(

                array('sales_flat_order_address'),

                'sales_flat_order.billing_address_id
= sales_flat_order_address.entity_id'
,

                array('postcode','street','city');

The second step is to add address column to the grid:


1

2

3

4

addColumn('address',array(

          'header'=>
Mage::helper(
'sales')->__('Address'),

          'type' =>'text',

));

Here we should connect column with the data. Let’s add ‘renderer‘
param:


1

2

3

4

5

6

addColumn('address',array(

          'header'=>
Mage::helper(
'sales')->__('Address'),

          'type' =>'text',

          'index'=>'city'

      'renderer'=>'Atwix_Ordersgrid_Block_Adminhtml_Sales_Order_Renderer_Address'

));

Third, create the renderer:


1

2

3

4

5

6

7

8

9

10

11

12

classAtwix_Ordersgrid_Block_Adminhtml_Sales_Order_Renderer_Address

    extendsMage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract

{

    publicfunctionrender(Varien_Object
$row)

    {

        $value=$row->getData('city')
.
','.

            $row->getData('street')
.
','.

            $row->getData('postcode');

 

        return$value;

    }

}

After these steps we are able to see the new column in the orders grid. But when you try to use filter on this column – you will get
the result, filtered by ‘city’ db column, since we have added city as
an index. To solve the problem we need to do one more thing: create callback method for the filter. The final view of addColumn calling
is going to be like the following one:


1

2

3

4

5

6

$this->addColumn('address',array(

            'header'=>
Mage::helper(
'sales')->__('Address'),

            'type' =>'text',

            'renderer'=>'Atwix_Ordersgrid_Block_Adminhtml_Sales_Order_Renderer_Address',

            'filter_condition_callback'=>array($this,'_addressFilter'),

));

Note, that we have removed index field – we don’t need it anymore. And then, we create callback method which is described below:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

protectedfunction_addressFilter($collection,$column)

    {

        if(!$value=$column->getFilter()->getValue())
{

            return$this;

        }

 

        $this->getCollection()->getSelect()->where(

            "sales_flat_order_address.city
like ?

            OR
sales_flat_order_address.street like ?

            OR
sales_flat_order_address.postcode like ?"

        ,"%$value%");

 

 

        return$this;

    }

As you can see, we are using simple db query condition to filter query results by the columns. This way you can create your own
filters for columns with complex values or custom renderers. Feel free to ask any questions and suggest your own improvements.

原文:http://www.atwix.com/magento/grid-filter-for-columns/

PS:在magento的后台grid,用“renderer”来处理最终显示的内容是很常用的一种做法,但这会带来一个问题是,经过“renderer”的那一列没法像普通的列一样用过滤,这篇文章就是针对这个问题给出了解决方案,而且用的还是原生结构就支持的方式,这里的关键词就是“filter_condition_callback”,至于为什么可以这样用,可以看下Core_Adminhtml_Block_Widget_Grid里的_addColumnFilterToCollection方法,这里不再详细解释

时间: 2024-11-04 22:52:26

Magento - GRID FILTER FOR COLUMNS WITH COMPLEX VALUES的相关文章

EXTJS4 Grid Filter 插件的使用 与后台数据解析------Extjs 查询筛选功能的实现

  先汗一个,一个小功能又踢腾了一天.本来这个带Demo的,但是上面介绍的不是很详细.用的时候问题不大,主要问题在文件导入方面.以为这个插件的使用和其他的不一样. 1.首先是需要引入文件的位置:如图 需要把整个grid都考到vs下,vs中结构如下: 2.设置路径,将文件导入 Ext.Loader.setConfig({ enabled: true }); Ext.Loader.setPath('Ext.ux', '../ext-js4.2/ux'); Ext.require([ '*', 'Ex

Magento 添加后台管理

后台菜单显示点击后404,如果adminhtml.xml配置正确,那是config.xml的问题 Magento Grid关联了多表后,表与表之间有相同字段出现.在后台点查询时出现报错解决用filter_index Java代码   $this->addColumn('name', array(       'header' => '返利商家',       'align' => 'right',       'width' => '50px',       'index' =&g

从零开始学_JavaScript_系列(十)——dojo(3)(GRID表格创建、样式、列宽可变、排序、过滤器)

如果没有接触过dojo,建议阅读: http://blog.csdn.net/qq20004604/article/details/51028702 里面介绍了如何加载dojo.(当然,本篇也考虑了未使用过dojo的人,可以只阅读该链接关于dojo下载的部分,以获得dojo) 关于dojo的下载,请查看: https://dojotoolkit.org/download/ 建议下载FULL SOURCE版 如果需要讨论,请评论.或者站内信,我会尽快回复. (34)gridx gridx系列插件并

《Ext JS 4 First Look》翻译之五:Grid、Tree和Form   

5.1. Grid panel      Grid应该是我们在开发时使用的最多的组件之一.Extjs4对其进行了重大的改进.      Extjs4与Extjs3的Grid生成不同的HTML.Sencha称其为智能渲染(Intelligent Rendering).Extjs3中即使不需要Grid的所有特性,它也会生成整套HTML.而Extjs4就只会渲染Grid所用到的特性,这样就使 渲染最小化且提高了性能.      在学习Extjs4中Grid的新特性前,让我们先了解在Extjs4中如何创

《Ext JS 4 First Look》翻译之五:Grid、Tree和Form

<Ext JS 4 First Look>翻译之五:Grid.Tree和Form      至此我们已经学习了Data包和布局等API.下面我们来学习作为Extjs框架中我们用得最多的用来展现数据的Grid.Tree和Form吧! 目录: 5.1. Grid panel 5.1.1. Columns 5.1.2. Feature 5.1.2.1. Ext.grid.feature.Grouping 5.1.2.2. Ext.grid.feature.Summary 5.1.2.3. Ext.g

ExtJS入门教程06,grid分页的实现

前面两篇内容分别介绍了extjs grid的基本用法和extjs grid异步加载数据,这篇文章将介绍extjs grid的分页. 数据量大的时候我们必须用到分页,结合上一篇的异步加载数据,今天我们就看看如何异步的加载分页数据. 在extjs grid的请求中,包含几个参数,如图: page:当前页 start:起始行的行号 limit:每页数据行数,默认为25 在请求处理的时候,我们只要获得这些参数,就可以方便的将想要的分页后的数据返回给客户端. 接下来我们新建一个handler,用来处理分页

LigerUi中Grid控件如何让左侧第一列显示序号

代码示例如下: var grid = $("#maingrid").ligerGrid({ columns: [ { display: "序号", name: "ID", width: 100, type: "text", align: "left" }, { display: "联系电话", name: "Telephone", width: 100, type:

ExtJS入门教程04,这是一个超级好用的grid

今天进行extjs入门教程的第四篇:grid. 来一份grid尝尝 小伙伴们都知道extjs的grid功能强大,更清楚功能强大的东西用起来必然会复杂.今天我们就从最简单的grid开始讲解. 先来一个最简单的grid,让小伙伴们见识一下吧,看代码: Ext.onReady(function () { Ext.define('User', { extend: 'Ext.data.Model', fields: [ { name: 'name', type: 'string' }, { name: '

ExtJs之Ext.grid.GridPanel(部分未完)

今天在家休息,年假不用就作费啊. 看了几部香港老电影,陪爸爸看了勇士占奇才, 然后,测试了一下EXTJS未完的内容, 在京东上订了七本历史普及书,近两百块..:) 搞定. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57