使列网格过滤器接受 Magento 中的复杂值
Making Column Grid Filter Accept Complex Value In Magento
在 Magento 中,产品 Grid/Table 中有一列是名称。
它只接受或过滤 table 如果你输入准确的 word/s。
样本是"What Happened Last Night"(只是一个例子)
如果你输入 "What" 它会正确过滤,如果你添加 "Happened" 它仍然会正确过滤但是如果你输入 "What Last" 它会 return 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'),
));
}
基于此link、Grid filter for columns with complex values
我需要删除索引并调整一些我完全迷失的东西或一些文件,因为作者没有提到使用的文件路径。
任何人都可以指导我根据需要调整或创建什么文件。
上述代码的文件路径为
app\code\core\Mage\Adminhtml\Block\Catalog\Product\Grid.php
我还应该调整哪些 files/modules 以及我应该查看哪些代码行才能获得此结果。
我对你的帮助感激不尽。
Atwix 示例无法解决您描述的问题。如果您想使过滤器足够智能以成为即时标记器,您将必须按空格拆分搜索字符串,并将每个标记添加到查询的 "OR LIKE %'.$token.'%"
部分
解决方案
您将不得不在本地模块中重写 Mage_Adminhtml_Block_Catalog_Product_Grid,如下 xml:
/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>
并将以下方法添加到 class
/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/
不客气
这是适合您的情况的有效解决方案
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;
}
这个 space 分词器过滤器也适用于任何其他网格字段。
在 Magento 中,产品 Grid/Table 中有一列是名称。
它只接受或过滤 table 如果你输入准确的 word/s。
样本是"What Happened Last Night"(只是一个例子)
如果你输入 "What" 它会正确过滤,如果你添加 "Happened" 它仍然会正确过滤但是如果你输入 "What Last" 它会 return 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'),
));
}
基于此link、Grid filter for columns with complex values
我需要删除索引并调整一些我完全迷失的东西或一些文件,因为作者没有提到使用的文件路径。
任何人都可以指导我根据需要调整或创建什么文件。
上述代码的文件路径为
app\code\core\Mage\Adminhtml\Block\Catalog\Product\Grid.php
我还应该调整哪些 files/modules 以及我应该查看哪些代码行才能获得此结果。
我对你的帮助感激不尽。
Atwix 示例无法解决您描述的问题。如果您想使过滤器足够智能以成为即时标记器,您将必须按空格拆分搜索字符串,并将每个标记添加到查询的 "OR LIKE %'.$token.'%"
部分解决方案
您将不得不在本地模块中重写 Mage_Adminhtml_Block_Catalog_Product_Grid,如下 xml:
/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>
并将以下方法添加到 class
/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/
不客气
这是适合您的情况的有效解决方案
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;
}
这个 space 分词器过滤器也适用于任何其他网格字段。