Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter in grid with custom renderer

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.

like image 615
MiMo Avatar asked Jan 04 '13 00:01

MiMo


1 Answers

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;
}
like image 105
Scruffy Paws Avatar answered Sep 24 '22 02:09

Scruffy Paws