I have added a custom grid column using the field URL in a custom database:
$this->addColumn('url', array(
'header' => Mage::helper('companymodule')->__('Has Website'),
'align' => 'left',
'index' => 'url',
'renderer' => new Company_Module_Block_Adminhtml_Module_Grid_Renderer_HasUrl(),
'type' => 'options',
'options' => Mage::getSingleton('companymodule/hasurl')->getOptionArray(),
));
And created this custom rendered:
class Company_Module_Block_Adminhtml_Module_Grid_Renderer_HasUrl extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$value = trim((string)$row->getData($this->getColumn()->getIndex()));
if (empty($value)) {
return 'No';
} else {
return 'Yes';
}
}
}
As well as a custom model for the filter dropdwon:
class Company_Module_Model_Hasurl extends Varien_Object
{
static public function getOptionArray()
{
return array(
'Yes' => Mage::helper('companymodule')->__('Yes'),
'No' => Mage::helper('companymodule')->__('No')
);
}
}
This works fine unless you try to filter. I think it's trying to match the value 'Yes' to the value of $row->getData($this->getColumn()->getIndex())
(the URL field in the database table.) But instead of matching the URL field value itself, I am basically trying to filter by ((bool)empty($url))
so that the user can filter by whether or not this database entry has a URL or not.
How can I create custom logic for a filter like this?
Best Answer
try adding a custom filter callback:
in your grid:
and add a method like this:
note it's not tested; you may have to check for empty rather than null on the database; also make sure you have the correct table alias (if URL is not on the main_table).