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
158 0
如何处理APF框架的错误消息:Filter is too complex error
|
1月前
|
机器学习/深度学习 数据挖掘
【博士每天一篇文献-综述】Communication dynamics in complex brain networks
本文综述了复杂脑网络中的通信动态,提出了一个将通信动态视为结构连接和功能连接之间必要联系的概念框架,探讨了结构网络的局部和全局拓扑属性如何支持网络通信模式,以及网络拓扑与动态模型之间的相互作用如何提供对大脑信息转换和处理机制的额外洞察。
31 2
【博士每天一篇文献-综述】Communication dynamics in complex brain networks
|
1月前
|
机器学习/深度学习
【文献学习】Exploring Deep Complex Networks for Complex Spectrogram Enhancement
介绍了一种用于语音增强的复数深度神经网络(CDNN),它通过复数值的短时傅立叶变换(STFT)映射到干净的STFT,并提出了参数整流线性单位(PReLU)的复数扩展,实验结果表明CDNN在语音增强方面相对于实值深层神经网络(DNN)具有更好的性能。
34 2
【文献学习】Exploring Deep Complex Networks for Complex Spectrogram Enhancement
|
1月前
|
数据可视化 算法 Go
【博士每天一篇文献-实验】Exploring the Morphospace of Communication Efficiency in Complex Networks
这篇论文探讨了复杂网络中不同拓扑结构下的通信效率,并使用"效率形态空间"来分析网络拓扑与效率度量之间的关系,得出结论表明通信效率与网络结构紧密相关。
34 3
|
1月前
|
机器学习/深度学习 网络协议 PyTorch
【文献学习】DCCRN: Deep Complex Convolution Recurrent Network for Phase-Aware Speech Enhancement
本文介绍了一种新的深度复数卷积递归网络(DCCRN),用于处理语音增强问题,特别是针对低模型复杂度的实时处理。
27 5
|
1月前
|
机器学习/深度学习 PyTorch 算法框架/工具
【文献学习】Phase-Aware Speech Enhancement with Deep Complex U-Net
文章介绍了Deep Complex U-Net模型,用于复数值的语音增强,提出了新的极坐标掩码方法和wSDR损失函数,并通过多种评估指标验证了其性能。
31 1
|
1月前
|
机器学习/深度学习 算法 TensorFlow
【文献学习】Analysis of Deep Complex-Valued Convolutional Neural Networks for MRI Reconstruction
本文探讨了使用复数卷积神经网络进行MRI图像重建的方法,强调了复数网络在保留相位信息和减少参数数量方面的优势,并通过实验分析了不同的复数激活函数、网络宽度、深度以及结构对模型性能的影响,得出复数模型在MRI重建任务中相对于实数模型具有更优性能的结论。
23 0
【文献学习】Analysis of Deep Complex-Valued Convolutional Neural Networks for MRI Reconstruction
|
1月前
|
机器学习/深度学习 存储 算法
【文献学习】Deep Complex Networks
本文深入探讨了深度复数网络(Deep Complex Networks),包括其创新点、复数的优势、作者贡献,以及深度复数技术的具体应用,如复数卷积、激活函数、Batch-Normalization、权重初始化和卷积残差网络,并提出了对文中一些复杂概念的疑问和反思。
40 0
|
4月前
【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
121 0

热门文章

最新文章