Adding Filtering Functionality to Grid Columns in Magento
In this post, we’ll guide you through adding filtering functionality to grid columns in your Magento extension, focusing on the “Sources” column.
Current Implementation Overview
You’ve already set up the necessary collection joins and column definition:
protected function _prepareCollection()
{
// ... (existing code)
$collection->joinAttribute('source', 'customer/source', 'default_billing', null, 'left');
// ...
}
$this->addColumn('source', array(
'header' => Mage::helper('customer')->__('Source'),
'type' => 'source',
'index' => 'source'
));
This code joins the “source” attribute to your customer collection and defines a column for it in the grid. However, you’re missing the actual filter implementation.
Adding Filters to Grid Columns
To add filtering functionality, you need to utilize Magento’s built-in filters by extending the core Mage_Adminhtml_Block_Widget_Grid class.
Here’s how to do it:
- Extend the core grid block: Create a new block in your extension that extends
Mage_Adminhtml_Block_Widget_Grid.
class Mage_Custom_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
// ... (other code)
}
- Override the _prepareCollection() method: In your new block, override this method to add filtering conditions.
protected function _prepareCollection()
{
$collection = parent::_prepareCollection();
$this->setCollection($collection);
return $this;
}
protected function _addColumnFilterToCollection($column)
{
if ($column->getId() == 'source') {
$field = $column->getFilterIndex();
$condition = $this->getRequest()->getParam('filter');
if (!is_null($condition)) {
$collection = $this->getCollection()
->addFieldToFilter($field, array(
'like' => "%$condition%"
));
}
}
return parent::_addColumnFilterToCollection($column);
}
In this override, we check if the “source” column is being filtered. If so, we add a like condition to the collection to filter by the input value.
- Update your existing code: Replace your original grid block with the new one in your layout XML files:
<block type="magentocustom/adminhtml_customer_grid" name="customer.grid">
<action method="setSaveParametersInSession"><param>filter</param><param>select</param></action>
<block type="page/html_pager" name="customer.grid.pager"/>
</block>
- Update your grid setup: Ensure you have the necessary filter input in your layout XML:
<reference name="customer.grid">
<block type="core/text_list" name="grid.filter.form" as="grid_filter_form" before="-">
<action method="setTemplate"><template>grid/form/filter.phtml</template></action>
</block>
</reference>
Testing Your Filter
After implementing these changes, you should see a filter input box for the “Sources” column in your grid. When you enter a value and click Apply, the grid will update to display only customers with that source.
Remember, this is a basic example. In a real-world scenario, you might want to add additional filters or customize the grid rendering further.
Conclusion
Adding filters to grid columns in Magento requires understanding how the core grid system works and extending it appropriately. By creating a new block that extends Mage_Adminhtml_Block_Widget_Grid and overriding key methods like _prepareCollection() and _addColumnFilterToCollection(), you can easily add filtering functionality to your custom grids.
For more detailed examples and advanced techniques, consider checking out the official Magento 2 Grid documentation on our blog.