网格列过滤器无法使用magento自定义模块

时间:2015-07-07 13:11:05

标签: magento filter grid adminhtml

我正在使用magento后端的自定义模块,此过滤器在使用过滤器calback时无法正常工作! 谁能建议我? 谢谢!

我尝试过这样的代码,

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;
    }
}

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

1 个答案:

答案 0 :(得分:3)

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

替换你的回调方法

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

$collection

以下是我的建议:

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

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

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

  

定义不工作。回调是否被调用?是回调   代码对结果没有影响? - Lee Saferite 1小时前

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;
}