Search code examples
magentogridadminrenderer

can't Filter or search product in grid with custom renderer


I have a problem with filter in my module in admin grid. I also used 'order_item_id' field in my custom table and fetching product name based on '(sales/order_item)' using renderer. I also using " 'filter_condition_callback' => array($this, '_productFilter') " but it cant' work My problem is: can't Filter for columns with custom renderer not working. When i search product name in column,it returns zero records. What i wrong in My Code ?

public function _prepareColumns()
{
   $this->addColumn('entity_id', array(
      'header'    => Mage::helper('adminhtml')->__('ID'),
      'align'     =>'right',
      'width'     => '50px',
      'index'     => 'entity_id',
    ));         

    $this->addColumn('order_item_id', array(
      'header'    => Mage::helper('adminhtml')->__('Product Name'),
      'align'     =>'right',
      'index'     => 'order_item_id',
      'renderer' => 'Test_Module1_Block_Adminhtml_Renderer_Product',
      'filter_condition_callback' => array($this, '_productFilter'),
    ));
    return parent::_prepareColumns();
}
protected function _productFilter($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

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


    return $this;
}

my renderer is

 class Test_Module1_Block_Adminhtml_Renderer_Product extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $order = Mage::getModel('sales/order_item')->load($row->getData('order_item_id'));

        return $order->getName();
    }
}

Solution

  • It is because of you have rendered id and you are searching with product name. So you need to change your productfilter function and put code for search the id in table according to product name. You need to change your function :

    protected function _productFilter($collection, $column)
    {
      if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }
     $orderitem = Mage::getModel('sales/order_item')->getCollection();
     $orderitem->addFieldToFilter('name',array('like'=>'%'.$value.'%'));  
       $ids =array();
      foreach($orderitem as $item){
          $ids[] = $item->getId();
     }
    
    $this->getCollection()->addFieldToFilter("id",array("in",$ids));
    return $this;
    }