Filter in grid with custom renderer

♀尐吖头ヾ 提交于 2019-12-03 13:27:17

问题


I have a problem with filter in my module in admin grid.

My problem is: Filter for columns with custom renderer not working.

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'
        ));

renderer is

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

Author in grid is showing fine for example 'George Bush', but if i try write to filter (for example 'Bu') filter return zero row. :-/

Any idea? Thx.


回答1:


This article may help... http://www.atwix.com/magento/grid-filter-for-columns/

On your addColumn() call for the custom field, add something like...

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

Then add the filter method (changing the "where()" as needed)...

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

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


    return $this;
}


来源:https://stackoverflow.com/questions/14149409/filter-in-grid-with-custom-renderer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!