2

I created a column for my grid using renderer to change id to name like this:

$this->addColumn(
            'city_destination',
            array(
                'header' => Mage::helper('test_shippingcost')->__('City Destination'),
                'index'  => 'city_destination',
                'type'=> 'text',
                'renderer'  => 'Test_ShippingCost_Block_Adminhtml_Renderer_City'
            )
        );

But when i want try to filter the value using text, it won't budge unless i'm using id

renderer:

class Test_ShippingCost_Block_Adminhtml_Renderer_City extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
  public function render(Varien_Object $row)
  {
    $id =  $row->getData($this->getColumn()->getIndex());
    $collection = Mage::getModel('test_shippingcost/city')->load($id);
    $value = $collection->getName();
    return $value;
  }
}
4

1 Answer 1

1

This may help

https://www.atwix.com/magento/grid-filter-for-columns/

Add a filter_condition_callback

$this->addColumn(
                'city_destination',
                array(
                    'header' => Mage::helper('test_shippingcost')->__('City Destination'),
                    'index'  => 'city_destination',
                    'type'=> 'text',
                    'renderer'  => 'Test_ShippingCost_Block_Adminhtml_Renderer_City'
                    'filter_condition_callback' => array($this, '_filterCityDestinationCallback')
                )
            );

Then add

   protected function _filterCityDestinationCallback($collection, $column) {
        $value= $column->getFilter()->getValue();
        if (!$value) {
              return $this;
        }
        $this->getCollection()->getSelect()->where("main_table.name like ?","%$value%");                   
        return $this;
   }
2
  • the city name is not located in main_table, so table city is connected to the main_table by using foreign key Oct 4, 2017 at 7:31
  • You can use $value (name) to get the ids and filter the collection with them, but why don't you get the city destination name in your collection in the first place? You won't be needing a renderer or a custom filter. Oct 4, 2017 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.