使用自定义渲染器在网格中过滤

时间:2013-01-04 00:21:02

标签: magento magento-1.7

我的管理网格模块中的过滤器存在问题。

我的问题是: 过滤自定义渲染器不起作用的列。

public function _prepareColumns()
    {
        $this->addColumn('entity_id', array(
            'header' => 'ID',
            'index'  => 'entity_id',
            'width'  => '30px'
        ));
        $this->addColumn('author', array(
            'header'   => 'Author',
            'index'    => 'author',
            'renderer' => 'Test_Block_Adminhtml_Vj_Renderer_Author'
        ));

渲染器

class Test_Block_Adminhtml_Vj_Renderer_Author extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $value = $row->getData($this->getColumn()->getIndex());
        $autor = Mage::getModel('test/test')->load($value);
        return ($author->getName() . ' ' . $author->getSurname());
    }
 }

网格中的作者显示例如'乔治布什',但如果我尝试写过滤(例如'Bu')过滤器返回零行。 : - /

有什么想法吗? THX。

1 个答案:

答案 0 :(得分:14)

这篇文章可能有所帮助...... http://www.atwix.com/magento/grid-filter-for-columns/

在自定义字段的addColumn()调用中,添加类似...

的内容

'filter_condition_callback' => array($this, '_myCustomFilter'),

然后添加过滤方法(根据需要更改“where()”...

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

    $this->getCollection()->getSelect()->where(
        "my_field like ?"
    , "%$value%");


    return $this;
}