8

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?

  • try to change the code like this 'renderer' => Company_Module_Block_Adminhtml_Module_Grid_Renderer_HasUrl – MeenakshiSundaram R May 23 '14 at 5:02
17

try adding a custom filter callback:

in your grid:

$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(),
    'filter_condition_callback' => array($this, '_filterHasUrlConditionCallback')
));

and add a method like this:

protected function _filterHasUrlConditionCallback($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }
    if (empty($value)) {
        $this->getCollection()->getSelect()->where(
             "main_table.url IS NULL");
    }
    else {
        $this->getCollection()->getSelect()->where(
             "main_table.url IS NOT NULL");
    }

    return $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).

  • Thank you! For some reason if I have 'filter'=>true in the addColumn array I get a fatal error, but without it, this works great. Any idea why I can't have 'filter'=>true ? – Tyler V. Jun 3 '14 at 0:36
  • From what I can gather, you only specify a boolean in the 'filter' property when you want to tell Magento not to filter that column (i.e., 'filter' => false). Otherwise, you would put the name of a block that is used as a filter. See the class Mage_Adminhtml_Block_Review_Grid_Filter_Type for an example. – Laura Jun 3 '14 at 19:14
3

try adding a custom filter callback: Here we add new Role Name column to admin user grid.

in your grid:

$this->addColumn('role_name', array(
    'header' => Mage::helper('adminhtml')->__('Role Name'),
    'index' => 'role_name',
    'type' => 'options',
    'options' => $this->getRoleName(),
    //'filter' => false,
    'renderer' =>  'Company_Module_Block_Adminhtml_Permissions_User_Grid_Renderer_Role',
    'filter_condition_callback' => array($this, '_roleFilter'),
));

and add a 2 method like this:

protected function _roleFilter($collection, $column) {
    $filterroleid = $column->getFilter()->getValue();        
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }        
    $this->getCollection()->addFieldToFilter('parent_id', array('eq' => $filterroleid));
    return ;
}

Add This method for Role Filter with drop-down

public function getRoleName() {
    $rolename = array();
    $roles = Mage::getModel('admin/roles')->getCollection();
    foreach ($roles as $role):
        $rolename[$role->getId()] = $role->getRoleName();
    endforeach;
    return $rolename;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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