Changing Row Colors for Groups on Magento Admin Grid
Are you facing challenges while trying to apply different background colors for specific groups of rows on your Magento backend grid? You’re not alone. Many developers struggle to achieve this desired effect beyond the default row coloring.
The Challenge: Grouping Rows by Column Value
By default, Magento applies a light and dark color scheme to alternate rows in the grid view. However, you need a more granular controlâyou want to change the background color for entire groups of rows based on a known column value.
For instance, if your grid displays orders, you might want to highlight all ‘Pending’ status orders with one color and ‘Completed’ orders with another. The challenge is that the SO solution you found only applies an alternate row coloring, not grouping by column values.
Understanding the Grid Rendering Process
To solve this issue, it’s crucial to understand how Magento renders the grid view in the admin area. It uses a block called Magento_Theme\Block_Adminhtml_Template to render the content of the blocks and templates.
The key lies in extending this block in your custom module to manipulate the CSS used for rendering the grid rows.
Solution: Override Grid Row Rendering
To apply different colors to groups of rows, you need to override the Magento\Backend\Block\Widget\Grid class. Here’s a step-by-step guide:
- In your custom module’s
di.xmlfile, add a<preference>node that extends theMagento\Backend\Block\Widget\Gridblock with your own implementation.
<preference for="Magento\Backend\Block\Widget\Grid" type="Vendor\ModuleName\Block\Adminhtml\MyGrid" />
-
Create a new class called
MyGrid.phpin the specified path, extending theMagento\Backend\Block\Widget\Gridclass. -
In your custom grid block, override the
_prepareLayout()method to add JavaScript and CSS files that will handle the row coloring.
protected function _prepareLayout()
{
parent::_prepareLayout();
$this->pageConfig->addBodyClass('pages-grid');
$this->pageConfig->getTitle()->prepend($this->getTitle());
// Add custom scripts and styles for row coloring
$this->_view->getPage()->getConfig()->addJs(
'Vendor_ModuleName:js/row-coloring.js'
);
$this->_view->getPage()->getConfig()->addCSS(
'Vendor_ModuleName::css/grid-rows.css'
);
return $this;
}
-
In your JavaScript file (
row-coloring.js), use jQuery to select the rows based on their column values and apply the desired CSS classes. -
In your CSS file (
grid-rows.css), define the styles for these classes, setting the background colors accordingly.
.pending-row {
background-color: #f2dede;
}
.completed-row {
background-color: #d9edf7;
}
Conclusion
Changing the row colors for groups in Magento admin grids requires a deep understanding of the grid rendering process and customization of the grid block. By extending the Magento\Backend\Block\Widget\Grid class and adding custom scripts and styles, you can apply different color schemes to specific groups of rows based on their column values.
Remember that for larger projects or complex requirements, it’s always a good idea to consult with experts or seek professional help. At Laravel Company, we specialize in Magento development and can provide tailored solutions for your e-commerce needs.