How to Create Custom grid in magento admin
In magento we will create and call our custom grid for our custom module from admin section. Follow below steps and show your custom grid in admin magento
1. Call your grid action from adminhtml.xml or config.xml.Here am calling my menu from adminhtml.xml. Am showing link from admin menu
<?xml version=”1.0″?>
<config>
<menu>
<mymodule>
<children>
<myblock module=”mymodule”>
<title>Myblock</title>
<sort_order>3</sort_order>
<action>mymodule/adminhtml_mycontroller</action>
……
…..
2. Create you controller inside Jute\Mymodule\controllers\Adminhtml\MycontrollerController.php. Modify code like below. When admin click the above menu, the link will redirect to below index action. In index action we had guide the magento to redirct and call block file.
class Jute_Mymodule_Adminhtml_MycontrollerController extends Mage_Adminhtml_Controller_Action
{
public function indexAction() {
$this->_title($this->__(‘Mymodule’))->_title($this->__(‘My Grid’));
$this->loadLayout();
$this->_setActiveMenu(‘yourmodule/yourmodule’); //this will help to make active class of your admin menu
$this->_addContent($this->getLayout()->createBlock(‘mymodule/adminhtml_mygrid’)); //here we are calling Mygrid.php file
$this->renderLayout();
}
public function gridAction()
{
$this->loadLayout();
$this->getResponse()->setBody($this->getLayout()->createBlock(‘mymodule/adminhtml_myblock_grid’)->toHtml()); //Here we are calling Grid.php from adminhtml/myblock folder path
}
}
3. Jute\Mymodule\Block\Adminhtml\Mygrid.php. Here we need to call our grid action from our controller
class Jute_Mymodule_Block_Adminhtml_Mygrid extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct() {
$this->_blockGroup = ‘mymodule’;
$this->_controller = ‘adminhtml_mycontroller’; //indexaction
$this->_headerText = Mage::helper(‘mymodule’)->__(‘My Gird Title’);
parent::__construct();
}
}
4. Create your Grid and fetch your custom table collection , Jute\Mymodule\Block\Adminhtml\Myblock\Mygrid.php
class Jute_Mymodule_Block_Adminhtml_Myblock_Mygrid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId(‘mygrid’);
$this->setDefaultSort(‘entity_id’); //this is your primary key column
$this->setDefaultDir(‘DESC’);
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$collection = Mage::getModel(‘mymodule/mytable’)->getCollection(); //as per magento we are calling our model (table)
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$currency = (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
$this->addColumn(‘entity_id’,
array(
‘header’ => ‘ID’,
‘align’ =>’right’,
‘width’ => ’50px’,
‘index’ => ‘entity_id’,
));
return parent::_prepareColumns();
}
public function getRowUrl($row)
{
return $this->getUrl(‘*/*/grid’, array(‘id’ => $row->getId()));
}
}
Clear cache and check the grid. You will get the grid as result. If you are followed grid and table structure as per magento. Else you won’t get this grid. So when you create a table, create via magento way also create collection files and model files we had done as per magento else you won’t get result.