简体   繁体   English

Magento管理产品网格自定义列过滤器不起作用

[英]Magento admin product grid custom column filter not working

I am using join collection of my custom table value with product collection in my magento store admin product grid.the problem is that i can't filter or sort that custom column with already inbuilt grid sorting and filtering functionality..i am retrieving custom value from renderer in grid..i have tried almost all things from google but still i am not able to get proper solution for that.i have also used filter_condition_callback in _prepareColumns() method of my column but it didnt make sense.so any help would be great. 我在magento商店管理产品网格中将自定义表值的连接集合与产品集合一起使用。问题是我无法使用已内置的网格排序和过滤功能对自定义列进行过滤或排序..我正在检索自定义值从网格中的渲染器..我已经尝试了谷歌的几乎所有东西,但仍然无法为此找到合适的解决方案。我还使用了我列的_prepareColumns()方法中的filter_condition_callback,但是没有任何意义。很棒。

Thanks 谢谢

I normally use an observer to add a column to the grid. 我通常使用观察器在网格中添加一列。 See this page (german language) how to do this. 请参阅此页面(德语)。

Also the full module of this tutorial is available on GitHub. GitHub上也提供了本教程的完整模块。

But to help you, a bit of source code would be fine. 但是为了帮助您,可以使用一些源代码。 Which custom column would you like to filter? 您要过滤哪个自定义列? Which values can it have? 它可以具有哪些值?

I did it like this (in file Observer.php ) 我是这样做的(在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'
);

and then also in Observer.php 然后在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM