【问题标题】:Making Column Grid Filter Accept Complex Value In Magento使列网格过滤器接受 Magento 中的复杂值
【发布时间】:2017-10-26 18:27:31
【问题描述】:

在 Magento 中,产品网格/表格中有一个列是名称。

只有输入准确的单词,它才会接受或过滤表格。

示例是“昨晚发生的事情”(只是一个示例)

如果您输入“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

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

对你的帮助我感激不尽。

【问题讨论】:

    标签: magento filter product


    【解决方案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;
    }
    

    此空间标记器过滤器也适用于任何其他网格字段。

    【讨论】:

      【解决方案2】:

      Atwix 示例无法解决您描述的问题。如果您想使过滤器足够智能以成为即时标记器,则必须将搜索字符串按空格拆分,并将每个标记添加到查询的位置,其中包含“OR LIKE %'.$token.'%”

      解决方案

      您必须使用以下 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/

      不客气

      【讨论】:

      • 您好,先生。不如不创建我自己的模块,而是为核心本身更新它?
      • 我更新了帖子,向您展示先生全名添加列
      • 在核心中进行更改不是一个好习惯。它可能会破坏更新能力。它是由你决定。你可以在任何你喜欢的地方使用我的 filter_condition_callback 方法。
      • 你能告诉我如何覆盖吗?这只是一个测试环境。我只是用它来深入了解magento。谢谢
      • 当然,我可以帮助你。我的回答已经是关于如何覆盖的完整指南。询问您是否还有其他问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多