是否可以将多个事件处理程序绑定到 JqGrid 事件而不覆盖以前的事件? [英] Is it possible to bind multiple event handlers to JqGrid events without overwriting previous ones?

查看:21
本文介绍了是否可以将多个事件处理程序绑定到 JqGrid 事件而不覆盖以前的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我在每次加载页面时调用我的默认设置,并绑定一个函数到 loadComplete 来为我的网格做一些基本的格式化.

For example, I call my default settings on every page load and bind a function to loadComplete to do some basic formatting for my grids.

在某些页面上,我想同时执行额外的逻辑,但如果我在网格定义中设置 loadComplete,它将覆盖我在默认值中设置的函数.

On some pages, I have additional logic I would like to execute at the same time, but if I set loadComplete in the grid definition, it will overwrite the function set in my defaults.

有没有办法绑定多个处理程序,或者其他一些执行所有必要代码的方法?提前致谢.

Is there a way to bind multiple handlers, or some other way of executing all the necessary code? Thanks in advance.

推荐答案

我想你问的是当前版本的 jqGrid 中存在的一个重要问题.很难在 jqGrid 中实现更多的 事件处理程序现在.对于使用 jqGrid 的小型项目,这个问题并不重要,但在您想要构建一些解释 jqGrid 的类的情况下可能很重要.您当前的解决方法没问题,但有您自己了解的限制.主要问题是您可以仅实现一个额外的 loadCompleteEx 处理程序.

I think you ask about an important problem existing in the current version of jqGrid. It's difficult to implement more as one event handler in jqGrid now. The problem is not important for small projects where you use jqGrid, but can be important in case that you want construct some classes which interprets jqGrid. Your current workaround is OK, but have the restriction which you understand yourself. The main problem that in the way you can implement only one more additional loadCompleteEx handler.

我建议您使用 jQuery.trigger 或更好的 jQuery.triggerHandler.您可以将它与 jQuery.bindjQuery.unbind.通过这种方式,您可以定义任意数量的事件处理程序,它们的工作方式类似于标准 jQuery 事件.

What I would suggest you is the usage of jQuery.trigger or better jQuery.triggerHandler. You can use it together with jQuery.bind and jQuery.unbind. In the way you can define any number of event handlers which works like standard jQuery events.

例如

var grid = $("#list");

grid.bind('jqGridLoadComplete',function(e,data) {
    alert ("in first jqGridLoadComplete event handler. Called for the page " +
           data.page);
});
grid.jqGrid({
    // jqGrid parameters
    // ...
    loadComplete: function(data) {
        alert("main load complete");
        $(this).triggerHandler("jqGridLoadComplete", data);
    }
});
grid.bind('jqGridLoadComplete.jqGrid',function(e,data) {
    alert ("in second jqGridLoadComplete event handler. Called for the page " +
           data.page);
});

在第二个 jQuery.bind 中,我使用了命名空间jqGrid"中的命名空间事件jqGridLoadComplete.jqGrid".因此,您可以使用您所知道的(以及您需要的)关于 jQuery 中标准事件的所有内容.

In the second jQuery.bind I used namespaced event 'jqGridLoadComplete.jqGrid' from the namespace 'jqGrid'. So you can use everything which you know (and which you need) about standard events in jQuery.

此处查看相应的演示.尝试切换网格中的页面或更改排序以调用loadComplete.

See the corresponding demo here. Try to switch the pages in the grid or change the sorting to call loadComplete.

我在 jqGrid 定义之后绑定第二个 jqGridLoadComplete .因为我的演示使用 datatype:'local'loadComplete 的第一次调用将在绑定执行之前.因此,在第一个 loadComplete 执行时,将不会调用第二个 jqGridLoadComplete 事件处理程序.所以你最好在 jqGrid 定义之前绑定你的事件处理程序.在这种情况下,您可以确定该事件将始终被调用.

I bind the second jqGridLoadComplete after the jqGrid definition. Because my demo uses datatype:'local' the first call of loadComplete will be before the binding executed. So at the first loadComplete execution will be the second jqGridLoadComplete event handler not called. So you should better to bind your event handler before the jqGrid definition. In the case you can be sure that the event will be always called.

更新: 重要!!!.如果您使用当前版本的 jqGrid,则 不需要在 loadComplete 内部显式触发 jqGridLoadComplete 事件.jqGrid 已经为你做这个.上面的代码写在之前相应的功能被包含在jqGrid的主要代码中.答案只是描述了如何在 jqGrid 中实现多个事件处理程序的主要思想.写完答案后,我向 trirand 发布了一些拉取请求,并建议在 jqGrid 中实现 jQuery 事件.从 4.3.2 版开始(请参阅此处) jqGrid 支持事件.

UPDATED: IMPORTANT!!!. One don't need to trigger jqGridLoadComplete event explicitly inside of loadComplete if you use the current version of jqGrid. jqGrid do this already for you. The above code was written before the corresponding functionality are included in the main code of jqGrid. The answer just described the main idea how multiple event handlers could be implemented in jqGrid. After writing the answer I posted some pull requests to trirand with my suggestion to implement jQuery events in jqGrid. Starting with the version 4.3.2 (see here) jqGrid support the events.

这篇关于是否可以将多个事件处理程序绑定到 JqGrid 事件而不覆盖以前的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆