Creating a Simple Admin Module Grid for Products in Magento
When developing admin modules in Magento, creating a grid view for displaying product data is a common requirement. In this post, we’ll guide you through the process of building a simple admin module that lists products and their stock levels using a Magento grid.
Understanding the Current Setup
You’ve already set up an IndexController to load a basic admin template (list.phtml) with a table containing product data retrieved from a collection. This is a great starting point for building your custom grid.
Here’s how your current controller looks:
class Creare_StockManagement_IndexController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
$this->loadLayout();
$this->_addContent($this->getLayout()->createBlock('adminhtml/template')
->setTemplate('stockmanagement/list.phtml'));
$this->_setActiveMenu('stock_menu/stockmanagement');
$this->renderLayout();
}
}
Building the Custom Grid
To create a grid for your admin module, you’ll need to follow these steps:
- Create the grid block: In the
app/code/local/Creare/StockManagement/Blockdirectory, create a new file calledStockManagement.php. This block will extendMage_Adminhtml_Block_Widget_Grid.
class Creare_StockManagement_Block_StockManagement extends Mage_Adminhtml_Block_Widget_Grid
{
protected function _prepareCollection()
{
$collection = Mage::getModel('catalog/product')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => Mage::helper('adminhtml')->__('ID'),
'index' => 'entity_id',
));
// Add more columns like this...
return parent::_prepareColumns();
}
}
- Add the grid to your layout: In your
list.phtmltemplate, replace the existing table with the following code:
<block type="adminhtml/widget_grid" name="stockmanagement.grid">
<action method="setSaveParametersInSession"><param>form_data</param><name>stockmanagement_grid</name></action>
<block type="stockmanagement/stockmanagement" name="grid">
<action method="setGridParameters">
<columnId>entity_id</columnId>
<!-- Add more column IDs here -->
</action>
</block>
</block>
- Configure the grid manager and renderer: In your
IndexController, modify the layout block creation to use the custom grid:
$this->_addContent($this->getLayout()->createBlock('core/multicall')->addObject(
$this->getLayout()->createBlock('stockmanagement/stockmanagement')
));
Enhancing Your Grid
Now that you have a basic grid set up, there are several ways to enhance it:
- Add filters and sorting options by extending the
_prepareColumnsmethod. - Include action buttons for editing or removing products.
- Implement pagination using
setPagerVisibility(true).
For more advanced customizations, consider exploring Magento’s built-in grid system documentation: Admin Grid Configuration.
Conclusion
Creating a simple admin module grid in Magento involves setting up an admin controller, creating a grid block that extends the Mage_Adminhtml_Block_Widget_Grid class, and adding it to your layout using the adminhtml/widget_grid type.
By following this guide, you should now be able to create a functional grid for displaying products in your custom stock management module. To learn more about Magento development best practices, check out Laravel Company’s Magento Development Resources.
Happy coding!