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
  • First of all what is your renderer doing? Then you may need to set a custom filter callback Commented Oct 4, 2017 at 6:55
  • this may help - stackoverflow.com/a/16591280/4349218
    – Piyush
    Commented Oct 4, 2017 at 6:56
  • You say you are "using ID". Where? Is your column an options list or a text? Commented Oct 4, 2017 at 6:59
  • @YiorgosMoschovitis look at my updated question Commented Oct 4, 2017 at 7:01

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 Commented 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. Commented 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.