Code magento admin grid layout

This tutorial explains how to code magento admin grid layout. Inside the block folder we need to create a grid.php file where we need to declare the grid layout. The customgrid.php file we need to specify the grid.php file location.Please refer the following image for the file structure.

create_magento_admin_grid_file_structure

Please click here to find the tutorial that explains how to create the magento admin grid controller, model, adminhtml xml and config files.

Click here to download the full code.

create_magento_admin_grid_tutorial

/*
app\code\local\Display\Customgrid\Block\Adminhtml\Customgrid.php
*/

<?php
class Display_Customgrid_Block_Adminhtml_Customgrid extends Mage_Adminhtml_Block_Widget_Grid_Container {

    public function __construct()
    {
        $this->_controller = 'adminhtml_customgrid'; //locations of the grid.php
        $this->_blockGroup = 'display_customgrid';
        $this->_headerText = Mage::helper('customgrid')->__('Manage Contact');
        $this->_addButtonLabel = Mage::helper('customgrid')->__('Add Contact');
        parent::__construct();
    }


}

/*
app\code\local\Display\Customgrid\Block\Adminhtml\Customgrid\Grid.php
*/

<?php
class Display_Customgrid_Block_Adminhtml_Customgrid_Grid extends Mage_Adminhtml_Block_Widget_Grid {

    /**
     * Display_Customgrid_Block_Adminhtml_Customgrid_Grid constructor.
     */

    public function __construct()
    {
        parent::__construct();
        $this->setId('customgridGrid'); // set’s the ID of our grid
        $this->setDefaultSort('contact_id'); // sorting column to use in our grid
        $this->setDefaultDir('ASC'); // sorting order
        $this->setSaveParametersInSession(true); // sets your grid operations in session
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('customgrid/displaygrid')->getCollection();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        $this->addColumn('contact_id', array(
            'header'    => Mage::helper('customgrid')->__('Id'),
            'align'     =>'left',
            'index'     => 'contact_id',
        ));

        $this->addColumn('created_at', array(
            'header'    => Mage::helper('customgrid')->__('Date'),
            'align'     =>'left',
            'index'     => 'created_at',
        ));

        $this->addColumn('name', array(
            'header'    => Mage::helper('customgrid')->__('Name'),
            'align'     =>'left',
            'index'     => 'name',
        ));

        $this->addColumn('email', array(
            'header'    => Mage::helper('customgrid')->__('Email'),
            'align'     =>'left',
            'index'     => 'email',
        ));

        $this->addColumn('telephone', array(
            'header'    => Mage::helper('customgrid')->__('Telephone'),
            'align'     =>'left',
            'index'     => 'telephone',
        ));

        $this->addColumn('comment', array(
            'header'    => Mage::helper('customgrid')->__('Description'),
            'width'     => '150px',
            'index'     => 'comment',
        ));

        return parent::_prepareColumns();
    }
}

Share this Tutorial