【问题标题】:Magento admin product grid custom column filter not workingMagento 管理产品网格自定义列过滤器不起作用
【发布时间】:2015-06-15 19:11:09
【问题描述】:

我在我的 magento 商店管理产品网格中使用我的自定义表值的连接集合与产品集合。问题是我无法使用已经内置的网格排序和过滤功能过滤或排序该自定义列..我是从网格中的渲染器中检索自定义值..我已经尝试了几乎所有来自谷歌的东西,但我仍然无法获得适当的解决方案。我还在我的专栏的 _prepareColumns() 方法中使用了 filter_condition_callback 但它没有意义。所以任何帮助都会很棒。

谢谢

【问题讨论】:

  • 你解决了吗?

标签: php magento-1.7 admin


【解决方案1】:

我通常使用观察者向网格中添加一列。 See this page (german language) how to do this.

Also the full module of this tutorial is available on GitHub.

但是为了帮助你,一些源代码就可以了。您要过滤哪个自定义列?它可以有哪些值?

我是这样做的(在文件 Observer.php 中)

$grid->addColumnAfter(
    'custom_column_code', // replace by your column code
    array(
        'header'                        => 'Label of your column',
        'index'                         => 'custom_column_code', // replace by your column code
        'type'                          => 'options',
        'renderer'                      => 'YourCompany_YourModule_YourRenderer',
        'filter_condition_callback'     => array($this, '_filterHasMyCustomConditionCallback')
        'options'                       => array( //these are your filter options
            '1'     => 'Yes',
            '0'     => 'No'
        ),
    ),
    'name'
);

然后也在 Observer.php

public function _filterHasMyCustomConditionCallback($collection, $column)
{
    $value = $column->getFilter()->getValue();

    if ($value == '0')
    {
        // join custom_column LEFT (inner join fails on NULL values) and filter by NULL
        $collection->addAttributeToFilter('custom_column_code', array(['null' => true]), 'left');
    }
    else
    {
        // filter all by NOT NULL
        $collection->addFieldToFilter('custom_column_code', ['notnull' => true]);
    }

    return $this;
}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
相关资源
最近更新 更多