How to Enable AJAX in Magento Admin Grid

Magento provides an ultra awesome framework for creating grids in admin panel. All the CRUD features are available in Magento admin grid. But sometimes you may want more. Like it may be nice to apply search or filter using AJAX in the admin grids. Again Magento provides an ultra easy way to apply AJAX in thee admin grids. Its just a three step process and your grid will start implementing AJAX.


Here are the steps for enabling AJAX in Maganeo admin grid:
1. Open the Grid.php at following location: app\code\local\<package>\<module>\Block\Adminhtml\<grid_name>\Grid.php. In the constructor function of this file, add the following lines at the end:
public function __construct()
{
    parent::__construct();
    ...
    // Used for AJAX loading
    $this->setSaveParametersInSession(true);
    $this->setUseAjax(true);
}

2. In this file, add the following function:
// Used for AJAX loading
public function getGridUrl()
{
    return $this->getUrl('*/*/grid', array('_current'=>true));
}

3. Now open app\code\local\<package>\<module>\controllers\Adminhtml\<grid_name>Controller.php and add the following function:
// Used for AJAX loading
public function gridAction()
{
    $this->loadLayout();
    $this->getResponse()->setBody(
        $this->getLayout()->createBlock('reporting/adminhtml_laseroutput_grid')->toHtml()
    );
}

This is it. Now you can see AJAX enabled in the admin module.

We would love to hear from you...

back to top