I'm using a grid in magento admin (extending Mage_Adminhtml_Block_Widget_Grid
) to display the content of a database table. I saw in some other modules that there's an option to be set in the constructor to use ajax reloads instead of page reloads
this is my constructor:
public function __construct() {
parent::__construct();
$this->setId('myGrid');
$this->setSaveParametersInSession(true);
$this->setVarNameFilter('my_filter');
$this->setUseAjax(true);
}
but when I click the filter of the grid and click the search button, it reloads the WHOLE page (including header, footer,...) INSIDE the grid div.
Any ideas why this is the case?
I found the solution:
First I had to add the following function to the grid class
public function getGridUrl()
{
return $this->getUrl('*/*/grid', array('_current'=>true));
}
each ajax call will then be sent to this url (and the corresponding controller action) rather then the pages url.
then I added the following bit to the layout XML file:
<module_controller_grid>
<reference name="root">
<block type="package/gridblockname" name="root" output="toHtml" />
</reference>
</module_controller_grid>
this overwrites the root element and displays only the grid block. hence my ajax call will only load the updated grid itself and won't include header, footer, etc.
You could also add a "gridAction" method to your admin controller instead of creating a layout xml
public function gridAction()
{
$this->loadLayout();
$this->getResponse()->setBody(
$this->getLayout()->createBlock('{Namespace}/adminhtml_{Module}/grid')->toHtml()
);
}
Assuming:
Block Path : /app/code/local/{Namespace}/{Module}/Block/Adminhtml/{Module}/Grid.php
Add follwing code into __construct() function in :
app/code/local/[Name_Space]/[Module_Name]/Block/Adminhtml/[Module_Name]/Grid.php file.
$this->setUseAjax(true);
Now Add following function at last of this file.
public function getGridUrl()
{
return $this->getUrl('*/*/grid', array('_current'=>true));
}
Now Add following function at last in :
app/code/local/[Name_Space]/[Module_Name]/controllers/Adminhtml/[Module_Name]Controller.php file.
public function gridAction()
{
$this->loadLayout();
$this->getResponse()->setBody(
$this->getLayout()->createBlock('[Module_Name]/adminhtml_[Module_Name]_grid')->toHtml()
);
}
Courtesy : Nirav Kadiya
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With