使列网格过滤器接受Magento中的复杂值

时间:2017-10-26 18:27:31

标签: magento filter product

在Magento中,Product Grid / Table中有一列是Name。

如果您输入确切的单词,它只接受或过滤表格。

示例是“昨晚发生了什么”(仅举例)

如果你输入“What”它会正确过滤,如果你添加“Happened”它仍会正确过滤但是如果输入“What Last”它会返回0条记录,除非有“What Last”记录。

我在核心本身有这个代码。

 $this->addColumn('name',
        array(
            'header'=> Mage::helper('catalog')->__('Name'),
            'index' => 'name',
            'filter_condition_callback' => array($this, '_customNameFilter'),
    ));

    $store = $this->_getStore();
    if ($store->getId()) {
        $this->addColumn('name_search', array(
            'header'=> Mage::helper('catalog')->__('Name in %s', $store->getName()),
            'type'  => 'text',
            'filter_condition_callback' => array($this, '_customNameFilter'),
        ));
    }

根据此链接Grid filter for columns with complex values

我需要删除索引并调整一些我完全丢失的文件,因为作者没有提到使用的文件路径。

任何人都可以指导我根据需要调整或创建哪个文件。

上面的代码的文件路径为

app\code\core\Mage\Adminhtml\Block\Catalog\Product\Grid.php

我应该调整哪些其他文件/模块我应该查看哪些代码行以获得此结果。

我不能非常感谢你帮助我。

2 个答案:

答案 0 :(得分:1)

这是针对您案例的工作解决方案

protected function _customNameFilter($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
    return $this;
} else if (preg_match('/\s+/', $value)) {

        $tokens = explode(' ', $value);

    foreach($tokens as $token) {
        $this->getCollection()->addAttributeToFilter($column->getData('index'), array('like' => '%'.$token.'%'));
    }             
} else {   
    $this->getCollection()->addAttributeToFilter($column->getData('index'), array('like' => '%'.$value.'%'));
}
return $this;
}

此空间标记化过滤器也可以与任何其他网格字段一起使用。

答案 1 :(得分:0)

Atwix示例无法解决您描述的问题。如果你想让一个过滤器足够聪明,成为动态标记器,你将不得不用空格分割搜索字符串,并将每个标记添加到查询部分的"或LIKE%'。 $令牌'%"

<强>解决方案

您必须使用以下xml在本地模块中重写Mage_Adminhtml_Block_Catalog_Product_Grid:

/app/code/local/Your/Module/etc/config.xml

<global>
    <blocks>
        <adminhtml>
            <rewrite>
                <catalog_product_grid>Your_Module_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
            </rewrite>
        </adminhtml>
    </blocks>
</global>

并将以下方法添加到类

/app/code/local/Your/Module/Block/Adminhtml/Catalog/Product/Grid.php


class Your_Module_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {

        protected function _prepareColumns()
        {
            $this->addColumn('name_search', array(
                'header'=> Mage::helper('catalog')->__('Name in %s', $store->getName()),
                'type'  => 'text',
                'filter_condition_callback' => array($this, '_customNameFilter'),
            ));
            return parent::_prepareColumns();
        }

        protected function _customNameFilter($collection, $column)
        {
            if (!$value = $column->getFilter()->getValue()) {
                return $this;
            }

            $tokens = explode(' ', $value);

            $where = "LIKE '%".$value."%'";
            foreach($tokens as $token) {
                $where .= " OR LIKE '%".$token."%'";
            }

            $this->getCollection()->getSelect()->where(
                "(at_name.value ?)"
            , $where);


            return $this;
        }
    }

并且不要忘记使用您自己的命名空间重命名Your_Module并将模块描述xml添加到/ app / etc / modules /

欢迎你