How to add column filter for each column in magento grid for custom module

How to add column filter for each column in magento grid for custom module

July 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add column filter for each column in magento grid for custom module

Adding Column Filters for Each Column in Magento Grid for Custom Module

Adding column filters to your custom Magento grid can greatly enhance the usability of your backend module. This allows administrators to filter data based on various parameters like ID, email, name, and more. If you’re a developer working with Magento, here’s a step-by-step guide to help you add filters for each column in the custom grid.

Understanding the Grid Setup

The provided code shows how to set up a basic grid using Mage_Adminhtml_Block_Widget_Grid. The _prepareCollection method is responsible for fetching data from the database via the collection. Each column is added using methods like addColumn, specifying various properties such as header, index (database field name), type, and more.

Adding Filters to Your Custom Grid

To add filters to your grid, you need to override the _prepareColumns method in your custom block class. This method is responsible for preparing the columns for the grid.

protected function _prepareColumns()
{
    parent::_prepareColumns();

    $this->addColumn('id', array(
        'header'        => Mage::helper('CartAbandoned')->__('Id'),
        'align'         => 'right',
        'width'         => '50px',
        'type'          => 'number',
        'index'         => 'entity_id',
    ));

    // Add your custom columns here, following the same format

    $this->addColumn('action',array(
        'header'    => Mage::helper('CartAbandoned')->__('Action'),
        'type'      => 'action',
        'getter'    => 'getId',
        'actions'   => array(
            array(
                'caption' => Mage::helper('CartAbandoned')->__('View Products'),
                'url'     => array('base'=>'*/*/edit'),
                'field'   => 'id'
            )
        ),
        'filter'    => false,
        'sortable'  => false
    ));

    $this->addColumnFilterToHead('entity_id', 'number');
    $this->addColumnFilterToHead('customer_email', 'text');
    // Add column filters for other columns here, replacing the placeholders

    return parent::_prepareColumns();
}

Specifying Column Filters

In the _prepareColumns method, after adding all your columns, you need to specify the filter types. You do this using methods like addColumnFilterToHead.

  • For numeric fields (like ID), use 'number'.
  • For text fields (like email or name), use 'text'.
  • If you have other field types, refer to Magento’s column filter documentation for the appropriate filter type.

Conclusion

Adding filters to your custom Magento grid is a crucial part of enhancing the user experience in the backend. By following this guide and understanding how to override the _prepareColumns method, you can easily add filters to each column in your custom grid.

Remember that the key lies in specifying the correct filter types based on your database field data. Always refer to Magento’s official documentation for more advanced filtering options if needed.

For more detailed examples and tutorials on Magento development, feel free to explore our blog at LaravelCompany.com. We provide extensive guides and best practices to help you master Magento development efficiently.

Checkout Indexing Database Extensions

Our network