Sometimes, you need a special column on your grid from another table or with some calculation.
It works well, column is displayed but filter doesn’t work.
Here a small example how to customize your filter :
On your grid class (Adin_Sponsorship_Block_Adminhtml_Sponsorship_Grid.class.php), override your _prepareColumns() function
protected function _prepareColumns()
{
$this->addColumn('sponsor_email', array(
'header' => Mage::helper('evian_customer')->__('Sponsor Email'),
'index' => 'sponsor_email',
'align' => 'center',
'filter_condition_callback' => array($this, '_filterSponsortEmail'),
)
);
}
The ‘filter_condition_callback’ option indicates to use as filter a specific method, on the current class ($this) the method _filterSponsortEmail().
So next step, create this method.
protected function _filterSponsortEmail($collection, $column)
{
if (!$value = trim($column->getFilter()->getValue())) {
return;
}
$this->getCollection()->addFieldToFilter('customer.email', array('like' => '%'.$value.'%'));
}
Yes, this is this simple.
If you use a datetime column, it’s just a bit more complicated, see below
$this->addColumn('created_at', array(
'header' => Mage::helper('evian_customer')->__('Date of sponsorship'),
'index' => 'created_at',
'align' => 'center',
'type' => 'datetime',
'filter_condition_callback' => array($this, '_filterDate'),
)
);
and the filter method
protected function _filterDate($collection, $column)
{
$filters = $column->getFilter()->getValue();
$from = $filters['from'];
$to = $filters['to'];
$this->getCollection()->addFieldToFilter('main_table.created_at', array('gteq' => $from->toString('yyyy-MM-dd')));
$this->getCollection()->addFieldToFilter('main_table.created_at', array('lteq' => $to->toString('yyyy-MM-dd')));
}