使用Magento Admin Grid中的MySQL计算值过滤文本类型的列 [英] Filtering a text-type column with MySQL-computed values in Magento Admin Grid

查看:150
本文介绍了使用Magento Admin Grid中的MySQL计算值过滤文本类型的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

setCollection()

 'refunded'=>新的Zend_Db_Expr(IF(qty_refunded> 0,'是','否')),

_prepareColumns()

  $ this-> addColumnAfter('refunded',array(
'header'=> Mage :: helper('helper') - > __('Refunded'),
'index'=>'refunded',
'type'= >'text',
),'qty');

如果要更改列中的yes值, 是然后过滤?

解决方案

Adminhtml网格列具有指定块类的过滤器属性。对于通常为 adminhtml / widget_grid_column_filter_select

的布尔型yes / no字段如果您的字段类型为选项,则会自动使用。

_prepareCollection()中试试这个:

 'refunded'=>新的Zend_Db_Expr(IF(qty_refunded> 0,1,0)),

code $ _prepareColumns()使用:

  $ this-> addColumnAfter('refunded',array(
'header'=> Mage :: helper('helper') - > __('Refunded'),
'index'=>'refunded' ,
'type'=>'options',
'options'=>数组(0 => $ this-> __('否'),1 => $ this- > __('是'))
),'qty');

这应该仍然会使您的值在列中显示为是和否使用适当的选项作为过滤器下拉菜单来获取选择。

仅仅这样做是不够的,因为具有计算值的列不能在MySQL的WHERE子句中直接引用。 Magento提供了两个选项来解决这个问题。

列过滤器块有一个方法 getCondition(),它返回一个将用于过滤集合的条件。例如,请参阅 Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Abstract :: getCondition()
因此,如果您需要自定义用于执行过滤器的SQL,请创建您自己的列过滤器块扩展 Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Select 并根据需要调整返回的条件,即使用相同的计算值来匹配。

您可以为列指定自定义过滤器像这样在 addColumn()定义:

  'type'=> 'options',
'options'=>数组(0 => $ this-> __('否'),1 => $ this-> __('是')),
'filter'=> 'your_module / adminhtml_widget_grid_column_filter_custom',

如果您偏好在Ma​​gento的ORM过滤器语法的限制之外工作,您可以使用过滤回调直接修改集合:

 'type'=> 'options',
'options'=>数组(0 => $ this-> __('否'),1 => $ this-> __('是')),
'filter_condition_callback'=>数组($ this,'_applyMyFilter'),

回调接收集合和列作为参数。下面是该方法的一个简单示例:

 受保护的函数_applyMyFilter(Varien_Data_Collection_Db $ collection,Mage_Adminhtml_Block_Widget_Grid_Column $列)
{
$ select = $ collection-> getSelect();
$ field = $ column-> getIndex();
$ value = $ column-> getFilter() - > getValue();
$ select-> having($ field = ?, $ value);
}

不用说,这两种方法(针对计算值进行过滤)在MySQL中效率非常低,但在这种情况下对您来说可能不是问题。


Say one column in grid has computed values:

setCollection():

'refunded' => new Zend_Db_Expr("IF(qty_refunded > 0, 'Yes', 'No')"),

_prepareColumns():

$this->addColumnAfter('refunded', array(
    'header' => Mage::helper('helper')->__('Refunded'),
    'index'  => 'refunded',
    'type'   => 'text',
), 'qty');

What and how must one change in order to have columns with "yes" values, in case admin types "yes" then filters?

解决方案

Adminhtml grid columns have a filter property which specifies a block class. For boolean yes/no fields that would usually be adminhtml/widget_grid_column_filter_select.
It would be used automatically if your field type would be 'options'.

Try this in _prepareCollection():

'refunded' => new Zend_Db_Expr("IF(qty_refunded > 0, 1, 0)"),

And in _prepareColumns() use:

$this->addColumnAfter('refunded', array(
    'header'  => Mage::helper('helper')->__('Refunded'),
    'index'   => 'refunded',
    'type'    => 'options',
    'options' => array(0 => $this->__('No'), 1 => $this->__('Yes'))
), 'qty');

This should still render your values as "Yes" and "No" in the Column, and you would get the select with the appropriate options as a filter dropdown.

This alone won't be enough since the column with the computed value can't be referenced directly in the WHERE clause by MySQL. Magento provides two options to work around that.

Column filter blocks have a method getCondition() which return a condition that will be used to filter the collection. See Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Abstract::getCondition() for an example.
So if you need to customize the SQL used to execute the filter, create your own column filter block extending Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Select and adjust the returned condition as needed, i.e. use the same computed value to match against.
Your custom filter can be specified for the column like this in the addColumn() definition:

    'type'    => 'options',
    'options' => array(0 => $this->__('No'), 1 => $this->__('Yes')),
    'filter'  => 'your_module/adminhtml_widget_grid_column_filter_custom',

If you prefere to work outside of the limitations of Magento's ORM filter syntax, you can modify the collections select directly by using a filter callback:

    'type'    => 'options',
    'options' => array(0 => $this->__('No'), 1 => $this->__('Yes')),
    'filter_condition_callback' => array($this, '_applyMyFilter'),

The callback receives the collection and the column as arguments. Here is a simple example for that method:

protected function _applyMyFilter(Varien_Data_Collection_Db $collection, Mage_Adminhtml_Block_Widget_Grid_Column $column)
{
    $select = $collection->getSelect();
    $field = $column->getIndex();
    $value = $column->getFilter()->getValue();
    $select->having("$field=?, $value);
}

Needless to say that both approaches (filtering against the computed value) is very inefficient in MySQL. But maybe that's no a problem for you in this case.

这篇关于使用Magento Admin Grid中的MySQL计算值过滤文本类型的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆