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
class Atwix_Ordersgrid_Block_Adminhtml_Sales_Order_Renderer_Address
     extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
     public function render(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
protected function _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方法,这里不再详细解释微笑


目录
相关文章
SAP APF框架错误消息Filter is too complex的处理
SAP APF框架错误消息Filter is too complex的处理
SAP APF框架错误消息Filter is too complex的处理
如何处理APF框架的错误消息:Filter is too complex error
如何处理APF框架的错误消息:Filter is too complex error
143 0
如何处理APF框架的错误消息:Filter is too complex error
|
6月前
|
XML JSON JavaScript
什么是 SAP UI5 框架的 Complex Parser
什么是 SAP UI5 框架的 Complex Parser
48 0
|
6月前
|
JavaScript 开发工具
SAP UI5 complex parser 只有在 1.26 版本之后才能使用
SAP UI5 complex parser 只有在 1.26 版本之后才能使用
27 0
|
JavaScript 开发工具
SAP UI5 complex parser 只有在 1.26 版本之后才能使用(2)
SAP UI5 complex parser 只有在 1.26 版本之后才能使用
SAP UI5 complex parser 只有在 1.26 版本之后才能使用(1)
SAP UI5 complex parser 只有在 1.26 版本之后才能使用
SAP CRM One Order里Complex Set的一个例子:Partner Set
SAP CRM One Order里Complex Set的一个例子:Partner Set
107 0
SAP CRM One Order里Complex Set的一个例子:Partner Set
|
15天前
【echarts报错】line series not exists,should be same with series name or data name
【echarts报错】line series not exists,should be same with series name or data name
|
19天前
|
索引 Python
row[i] = col[j] = TrueIndexError: list assignment index out of range
row[i] = col[j] = TrueIndexError: list assignment index out of range
成功解决A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,co
成功解决A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,co

热门文章

最新文章