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

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

问题描述

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



在某些页面上,我有额外的逻辑,我想在同一时间执行,但如果我在网格定义中设置loadComplete,它将覆盖我的默认设置的功能。



是否一种绑定多个处理程序的方法,还是其他一些执行所有必需代码的方法?感谢提前。

解决方案

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



我建议你使用 jQuery.trigger 或更好的 jQuery.triggerHandler 。您可以与 jQuery.bind jQuery.unbind 。在这种方式你可以定义任何数量的事件处理程序,像标准的jQuery事件。



例如,

  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中的标准事件。



看到相应的演示这里。尝试切换网格中的页面或更改排序以调用 loadComplete



我绑定第二个<$ jqGrid定义之后的c $ c> jqGridLoadComplete 。因为我的演示使用数据类型:'local'首先调用 loadComplete 执行绑定。所以在第一个 loadComplete 执行将是第二个 jqGridLoadComplete 事件处理程序未被调用。所以你最好在jqGrid定义之前绑定事件处理程序。在这种情况下,您可以确定该事件将始终被调用。



更新: 重要!!! 。一个不需要在 loadComplete 内显式触发 jqGridLoadComplete ,如果您使用当前版本的jqGrid。 jqGrid 已经为您执行了。上述代码在之前写成,相应的功能包含在jqGrid的主要代码中。答案刚刚描述了如何在jqGrid中实现多个事件处理程序的主要思想。写完答案之后,我发布了一些拉特勒到我的建议,以在jqGrid中实现jQuery事件。从4.3.2版开始(请参阅这里)jqGrid支持事件。


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.

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.

解决方案

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.

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.

For example,

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);
});

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.

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

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.

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天全站免登陆