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: appcodelocal<package><module>BlockAdminhtml<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 appcodelocal<package><module>controllersAdminhtml<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.

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

2 thoughts on “How to Enable AJAX in Magento® Admin Grid

  1. How can we use this response in over phtml file like I want that when someone choose category in admin side, then ajax call and response append in another tab like custom value how can I do this ?

Comments are closed.