Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - grid filter using ajax reloads the whole page

Tags:

php

magento

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?

like image 969
clem Avatar asked Dec 16 '11 12:12

clem


3 Answers

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.

like image 93
clem Avatar answered Oct 19 '22 04:10

clem


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

like image 33
Renon Stewart Avatar answered Oct 19 '22 05:10

Renon Stewart


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

like image 33
Nikhil_K_R Avatar answered Oct 19 '22 03:10

Nikhil_K_R