Magento - 使用ajax的网格过滤器重新加载整个页面

发布于 2024-12-21 16:36:35 字数 481 浏览 2 评论 0原文

我在 magento admin 中使用网格(扩展 Mage_Adminhtml_Block_Widget_Grid )来显示数据库表的内容。我在其他一些模块中看到,在构造函数中可以设置一个选项来使用 ajax 重新加载而不是页面重新加载
这是我的构造函数:

public function __construct() {
    parent::__construct();
    $this->setId('myGrid');
    $this->setSaveParametersInSession(true);
    $this->setVarNameFilter('my_filter');
    $this->setUseAjax(true);
}

但是当我单击网格过滤器并单击搜索按钮时,它会重新加载网格 div 内的整个页面(包括页眉、页脚...)。
有什么想法为什么会出现这种情况吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冬天旳寂寞 2024-12-28 16:36:35

我找到了解决方案:
首先,我必须将以下函数添加到网格类,

 public function getGridUrl()
 {
   return $this->getUrl('*/*/grid', array('_current'=>true));
 }

然后每个 ajax 调用将被发送到此 url(以及相应的控制器操作),而不是页面 url。
然后我将以下内容添加到布局 XML 文件中:

<module_controller_grid>
  <reference name="root">
    <block type="package/gridblockname" name="root" output="toHtml" />
  </reference>
</module_controller_grid>

这会覆盖根元素并仅显示网格块。因此我的 ajax 调用只会加载更新的网格本身,不会包含页眉、页脚等。

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.

浅唱ヾ落雨殇 2024-12-28 16:36:35

您还可以向管理控制器添加“gridAction”方法,而不是创建布局 xml

public function gridAction()
{
    $this->loadLayout();
    $this->getResponse()->setBody(
           $this->getLayout()->createBlock('{Namespace}/adminhtml_{Module}/grid')->toHtml()
    ); 
}

假设:

Block Path :
/app/code/local/{命名空间}/{模块}/Block/Adminhtml/{模块}/Grid.php

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

薄暮涼年 2024-12-28 16:36:35

将以下代码添加到 __construct() 函数中:

app/code/local/[Name_Space]/[Module_Name]/Block/Adminhtml/[Module_Name]/Grid.php 文件。

$this->setUseAjax(true);

现在添加以下代码该文件最后的函数。

public function getGridUrl()
  {
      return $this->getUrl('*/*/grid', array('_current'=>true));
  }

现在最后在:

app/code/local/[Name_Space]/[Module_Name]/controllers/Adminhtml/[Module_Name]Controller.php 文件中添加以下函数。

public function gridAction()
     {
        $this->loadLayout();
        $this->getResponse()->setBody(
               $this->getLayout()->createBlock('[Module_Name]/adminhtml_[Module_Name]_grid')->toHtml()
        );
     }

由 Nirav 提供卡迪亚

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

毅然前行 2024-12-28 16:36:35

Nirav Kadiya 几乎适合我,但我在管理控制器中创建这样的 gridAction 函数:

public function gridAction()
     {
        $this->loadLayout();
        $this->getResponse()->setBody(
               $this->getLayout()->createBlock('[Module_Name]_Block_Adminhtml_[Model_Name]_Grid')->toHtml()
        );
     }

并将其包含在 de Grid 文件中

public function getGridUrl()
    {
          return $this->getUrl('*/*/grid', array('_current'=>true));
    }

The Nirav Kadiya almost works for me but I create the gridAction function like this in the Admin Controller:

public function gridAction()
     {
        $this->loadLayout();
        $this->getResponse()->setBody(
               $this->getLayout()->createBlock('[Module_Name]_Block_Adminhtml_[Model_Name]_Grid')->toHtml()
        );
     }

And include this in de Grid file

public function getGridUrl()
    {
          return $this->getUrl('*/*/grid', array('_current'=>true));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文