Changing Magento Grid Column Alignment for Number Type Columns
Magento’s grid columns offer a wealth of customization options, but sometimes you’ll find that certain default behaviors can’t be overridden through the standard configuration. One such case is aligning number type columns in the grid.
The Default Behavior: Hard-Coded Center Align
When you create a number type column using the addColumn method and set the 'align' property to 'center', Magento typically respects this setting:
$this->addColumn(
'my_number',
array(
'header' => Mage::helper('bidashboard')->__('My number'),
'index' => 'my_number',
'total' => 'sum',
'align' => 'center', // This should work, right?
'type' => 'number',
'sortable' => false,
)
);
However, Magento has hard-coded the alignment for number type columns in the Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Number class:
class Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Number
{
public function renderCss()
{
return parent::renderCss() . ' a-right';
}
}
The a-right CSS class is added to all number type columns, effectively overriding any alignment settings provided in the grid configuration.
Overriding the Default Behavior: Extended Renderer
To change the alignment of your number type column, you’ll need to create an extended version of Magento’s default renderer. Here’s a step-by-step guide:
-
Create a new file named
Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Number_Custom.phpin your custom extension. -
Implement the
rendermethod as follows:
class Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Number_Custom extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Number
{
public function render(Varien_Object $row)
{
return parent::render($row);
}
}
This is a placeholder for our custom alignment logic.
-
Create an observer to replace the default renderer with your custom one:
-
In
config.xml, add an event observer that listens for thecore_block_abstract_to_html_afterevent and replaces the default number column renderer with your custom version:
<config>
<global>
<events>
<core_block_abstract_to_html_after>
<observers>
<your_module_core_block_abstract_to_html_after>
<type>singleton</type>
<class>Your_Module_Model_Observer</class>
<method>replaceNumberRenderer</method>
</your_module_core_block_abstract_to_html_after>
</observers>
</core_block_abstract_to_html_after>
</events>
</global>
</config>
- Implement the
replaceNumberRenderermethod in your observer:
public function replaceNumberRenderer($observer)
{
$block = $observer->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Widget_Grid) {
foreach ($block->getColumns() as $columnId => $column) {
if ('number' == $column['type'] && 'a-right' != $column['align']) {
$customRenderer = Mage::getSingleton('your_module/renderer_number_custom');
$block->setColumnDefault($columnId, array_merge($column, array('renderer' => $customRenderer)));
}
}
}
}
- Now you can align your number type column as desired:
$this->addColumn(
'my_number',
array(
'header' => Mage::helper('bidashboard')->__('My number'),
'index' => 'my_number',
'total' => 'sum',
'align' => 'left', // This will now work!
'type' => 'number',
'sortable' => false,
)
);
Conclusion
While Magento’s default grid behavior sometimes requires creative workarounds, extending the built-in renderer classes provides a powerful way to customize the grid appearance. By following this approach, you can achieve fine-grained control over your grid column alignments, even for hard-coded columns like the number type.
Remember to keep your custom code well-documented and organized to ensure easy maintenance in case of future updates or changes in Magento’s core behavior.
For more information on Magento extension development best practices and advanced customization techniques, don’t hesitate to explore our Magento tutorials at Laravel Company.