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方法,这里不再详细解释微笑


目录
相关文章
|
机器学习/深度学习 数据挖掘
【博士每天一篇文献-综述】Communication dynamics in complex brain networks
本文综述了复杂脑网络中的通信动态,提出了一个将通信动态视为结构连接和功能连接之间必要联系的概念框架,探讨了结构网络的局部和全局拓扑属性如何支持网络通信模式,以及网络拓扑与动态模型之间的相互作用如何提供对大脑信息转换和处理机制的额外洞察。
499 2
【博士每天一篇文献-综述】Communication dynamics in complex brain networks
|
机器学习/深度学习
【文献学习】Exploring Deep Complex Networks for Complex Spectrogram Enhancement
介绍了一种用于语音增强的复数深度神经网络(CDNN),它通过复数值的短时傅立叶变换(STFT)映射到干净的STFT,并提出了参数整流线性单位(PReLU)的复数扩展,实验结果表明CDNN在语音增强方面相对于实值深层神经网络(DNN)具有更好的性能。
497 2
【文献学习】Exploring Deep Complex Networks for Complex Spectrogram Enhancement
|
机器学习/深度学习 网络协议 PyTorch
【文献学习】DCCRN: Deep Complex Convolution Recurrent Network for Phase-Aware Speech Enhancement
本文介绍了一种新的深度复数卷积递归网络(DCCRN),用于处理语音增强问题,特别是针对低模型复杂度的实时处理。
1634 5
|
数据可视化 算法 Go
【博士每天一篇文献-实验】Exploring the Morphospace of Communication Efficiency in Complex Networks
这篇论文探讨了复杂网络中不同拓扑结构下的通信效率,并使用"效率形态空间"来分析网络拓扑与效率度量之间的关系,得出结论表明通信效率与网络结构紧密相关。
328 3
|
机器学习/深度学习 PyTorch 算法框架/工具
【文献学习】Phase-Aware Speech Enhancement with Deep Complex U-Net
文章介绍了Deep Complex U-Net模型,用于复数值的语音增强,提出了新的极坐标掩码方法和wSDR损失函数,并通过多种评估指标验证了其性能。
587 1
|
机器学习/深度学习 算法 TensorFlow
【文献学习】Analysis of Deep Complex-Valued Convolutional Neural Networks for MRI Reconstruction
本文探讨了使用复数卷积神经网络进行MRI图像重建的方法,强调了复数网络在保留相位信息和减少参数数量方面的优势,并通过实验分析了不同的复数激活函数、网络宽度、深度以及结构对模型性能的影响,得出复数模型在MRI重建任务中相对于实数模型具有更优性能的结论。
687 0
【文献学习】Analysis of Deep Complex-Valued Convolutional Neural Networks for MRI Reconstruction
|
机器学习/深度学习 存储 算法
【文献学习】Deep Complex Networks
本文深入探讨了深度复数网络(Deep Complex Networks),包括其创新点、复数的优势、作者贡献,以及深度复数技术的具体应用,如复数卷积、激活函数、Batch-Normalization、权重初始化和卷积残差网络,并提出了对文中一些复杂概念的疑问和反思。
933 0
|
3月前
|
弹性计算 Java Linux
新版阿里云 MC 我的世界联机等游戏云服务器配置价格表
选型时有几个关键点需要留意。第一,MC服务端极度依赖单核性能,选购实例优先高主频规格,单纯堆多核并不能显著改善TPS;第二,模组服内存一定要预留充足,大型整合包很容易把内存占满导致服务端崩溃;第三,安全组不要大范围开放所有端口,仅开放25565游戏端口和22SSH远程端口;第四,按量付费实例不使用的时候务必释放,仅停机仍然会持续扣费;第五,定期备份world存档文件夹,防止误删、磁盘故障导致地图丢失。
550 1
|
6月前
|
人工智能 缓存 自然语言处理
阿里云AI通用型节省计划A类、B类、C类主要包含哪些模型?扣费规则与不同类型折扣信息参考
阿里云AI通用型节省计划是面向大模型按量付费场景的折扣方案,用户承诺3/6/12/24个月等月消费额,即可享阶梯式折扣。模型分为三类:A类涵盖千问全系列(不含qwen3.6-max-preview)、开源模型及向量/排序/工具调用,最高可享6.8折;B类包含图像生成、语音合成、视频生成等多模态服务,最高5.3折;C类为qwen3.6-max-preview及DeepSeek、Kimi等三方直供模型,不支持抵扣。付费方式分全预付和零预付,承诺金额越高、周期越长折扣越大。此外新用户还可领取全模型通用抵扣券(20/100/500元档)及AI加速季满减优惠券,进一步降低使用成本。
|
6月前
|
JSON 自然语言处理 前端开发
【开源剪映小助手】项目概述
capcut-mate 是一款开源免费、支持独立部署的剪映自动化系统,基于 FastAPI 构建,深度融合大模型能力,提供草稿创建、素材编排、云端渲染、本地导出及智能编辑等全链路功能,助力内容创作者高效批量生产专业视频。(239字)

热门文章

最新文章