magento - 网格列过滤器不适用于 magento 自定义模块中的两个模型

标签 magento filter grid adminhtml

我正在使用 magento 后端的自定义模块,在使用过滤器回调时此过滤器不起作用! 有人可以建议我吗? 谢谢!

我尝试过一些这样的代码,

Grid.php

protected function _prepareCollection()
    {
     $collection = Mage::getModel('listings/listings')->getCollection();
     $this->setCollection($collection);
       return parent::_prepareCollection();
    }
protected function _prepareColumns()
    {     
        $this->addColumn("item_id", array(
            "header" => Mage::helper("linkmanagement")->__("ID"),
            "align" => "center",
            "type" => "number",
            "index" => "item_id",

        ));

        $this->addColumn("title", array(
            "header" => Mage::helper("linkmanagement")->__("Title"),
            "index" => "title"
        ));

        $this->addColumn("cat_ids", array(
            "header" => Mage::helper("linkmanagement")->__("Cat ID"),
            "align" => "center",
            "index" => "cat_ids",
            "renderer" => 'Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories',
            'filter_condition_callback' => array($this, '_categoriesFilter')
        ));

        $this->addColumn("url_key", array(
            "header" => Mage::helper("linkmanagement")->__("URL"),
            "index" => "url_key",
            "width" => "200px",
        ));

        $this->addColumn('status',
            array(
                'header' => Mage::helper('linkmanagement')->__('Status'),
                'index' => 'status',
                'type' => 'options',
                'options' => array('1' => Mage::helper('linkmanagement')->__('Active'),
                    '0' => Mage::helper('linkmanagement')->__('Inactive')),
            )
        );

        return parent::_prepareColumns();
    }

回调函数

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

        $this->getCollection()->getSelect()->where(
            "cat_ids ?"
            , "%$value%");
        return $this;
    }

Categories.php

class Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

    public function render(Varien_Object $row)
    {
        $value =  $row->getData($this->getColumn()->getIndex());
        // $value = 38,92
        $value = explode(',',$value);
        $collection = Mage::getModel("categories/categories")->getCollection()->addFieldToFilter('category_id',$value)->getData();
        foreach($collection as $col){
            $result[] = $col['title'];
        }
        $result = implode(',', $result);// $result = schools,colleges
        return $result;
    }
}

注意: 渲染器工作正常。, 只有过滤器回调函数不起作用!!!

最佳答案

我用我自己的渲染器检查了您的示例,并尝试检查您的回调。

替换为您的回调方法

$this->getCollection()->getSelect()

$collection

以下是我的建议:


Magento 过滤器假设不是 100% 相似。我的意思是,如果你想应用过滤器,你应该使用构造:

where("cat_ids LIKE ?", "%$value%");

我仍然不明白以下问题的答案:

Define not working. Is the callback ever being called? Is the callback code having no affect on the results? – Lee Saferite 1 hour ago

Mage::log('blabla', false, 'grid.log', true); 放在回调方法的开头。然后检查这个日志文件。如果它不为空 - 您的方法调用成功。


如果您的方法调用 - 尝试

Mage::log($collection->getSelectSQL(1), false, 'grid.log', true);

应用过滤器之前和之后。并尝试在 phpmyadmin 中运行这些查询。检查结果


尝试在 Mage_Adminhtml_Block_Sales_Order_Grid 类中应用这些更改

这是我所做的:

    $this->addColumn("cat_ids", array(
        "header" => Mage::helper('sales')->__('Ship to Name'),
        "align" => "center",
        "index" => "grand_total",
        "renderer" => "My_Class_.....",
        "filter_condition_callback" => array($this, '_categoriesFilter')
    ));

...

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

    $collection->getSelect()->where(
        "status LIKE ?", "%$value%"
    );

    return $this;
}

...

public function render(Varien_Object $row)
{
    $value =  $row->getData('increment_id') . $row->getData('status');
    return $value;
}

关于magento - 网格列过滤器不适用于 magento 自定义模块中的两个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31269519/

相关文章:

php - Magento - 以编程方式添加的 bundle 产品不适用于购物篮/前端

php - Azure 上适用于 Magento 的 MySQL 应用程序内

mysql - 使用串联名称来过滤结果,但不在表中显示名称

python - Tornado:是否有请求过滤器?

ios - 如何绘制一个多列的 UITableView?

matlab - 有没有办法使matlab网格线加粗?

mysql - 如何通过 mysql 在 magento 中获取客户名称、电子邮件和密码

Magento 打印页面布局

c# - 在 VSTS 中运行选择性测试类

css - 如果可用,如何 float Bootstrap 列