JQGrid:loadComplete在数据类型:函数时不触发 [英] JQGrid: loadComplete NOT firing when datatype: function

查看:532
本文介绍了JQGrid:loadComplete在数据类型:函数时不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我调用函数来加载网格数据,则不会触发loadComplete.我需要处理此事件,以便可以正确手动更新multiselect复选框.如果我在gridComplete中进行更新,则必须单击两次复选框以取消选中它.

If I call a function to load my grid data, loadComplete does not fire. I need to handle this event so I can manually update multiselect checkbox correctly. If I update in gridComplete, I have to click the checkbox twice to uncheck it.

推荐答案

在上一个问题中,您写道在服务器端使用WCF.在的情况下,您无需使用datatype作为功能.取而代之的是,您可以使用以下参数:

In your previous question you wrote that you use WCF on the server side. In the case you don't need to use datatype as function. Instead of that you can just use the following parameters:

datatype: "json",
ajaxGridOptions: { contentType: "application/json" },
serializeGridData: function (data) {
    return JSON.stringify(data);
}

为确保旧网络浏览器支持JSON.stringify,您应包括json2.js,可从

To be sure that JSON.stringify are supported in old web browsers you should include json2.js which you can load from here.

旧答案中,您可以找到更多代码示例(并下载演示),显示了如何将WCF与jqGrid一起使用.

In the old answer you can find more code examples (and download the demo) which shows how you can use WCF with jqGrid.

现在,我将回答您的原始问题:如果您将datatype用作函数,那么为什么loadComplete无法启动".简短的答案是:如果您将datatype用作函数您的代码,则负责调用loadComplete.

Now I will answer on your original question: "Why loadComplete does not fire" if you use datatype as function. The short answer is: if you use datatype as function your code is responsible for calling of loadComplete.

如果将datatype用作函数您的代码对jqGrid通常执行的某些操作负责.因此,首先您必须了解datatype函数应该做什么.文档中的示例(请参见此处)显示datatype作为函数的最简单但不完整的实现.更完整的代码示例如下所示:

If you use datatype as function your code is responsible to some things which jqGrid do typically. So first of all you have to understand what should the datatype function do. An example from the documentation (see here) shows the simplest, but not full, implementation of datatype as function. More full code example looks like the following:

$("#list").jqGrid({
    url: "example.php",
    mtype: "GET",
    datatype: function (postdata, loadDivSelector) {
        var ts = this,  // cache 'this' to use later in the complete callback
            p = this.p; // cache the grid parameters
        $.ajax({
           url: p.url,
           type: p.mtype,
           dataType: "json",
           contentType: "application/json",
           data: JSON.stringify(postdata),
           cache: p.mtype.toUpperCase() !== "GET",
           beforeSend: function (jqXHR) {
               // show the loading div
               $($.jgrid.jqID(loadDivSelector)).show();
               // if loadBeforeSend defined in the jqGrid call it
               if ($.isFunction(p.loadBeforeSend)) {
                   p.loadBeforeSend.call(ts, jqXHR);
               }
           },
           complete: function () {
               // hide the loading div
               $($.jgrid.jqID(loadDivSelector)).hide();
           },
           success: function (data, textStatus, jqXHR) {
               ts.addJSONData(data);
               // call loadComplete
               if ($.isFunction(p.loadComplete)) {
                   p.loadComplete.call(ts, data);
               }
               // change datatype to "local" to support
               // "loadonce: true" or "treeGrid: true" parameters
               if (p.loadonce || p.treeGrid) {
                   p.datatype = "local";
               }
           },
           error: function (jqXHR, textStatus, errorThrown) {
               if ($.isFunction(p.loadError)) {
                   p.loadError.call(ts, jqXHR, textStatus, errorThrown);
           }
        });
    },
    ... // other parameters
});

您可以看到代码不那么短.在上面的示例中,我们仍然不支持某些jqGrid选项,例如虚拟滚动(scroll: 1scroll: true).

You can see that the code in not so short. In the above example we still not support some jqGrid options like virtual scrolling (scroll: 1 or scroll: true).

尽管如此,我希望我现在清除了为什么我不建议使用datatype作为函数.如果使用它,则必须了解许多内容 jqGrid在内部如何工作.您应该检查其源代码,以确保正确执行所有操作.如果您跳过某些内容,那么在某些情况下或在jqGrid参数的某些组合中,代码将无法正常工作.

Nevertheless I hope that I cleared now why I don't recommend to use datatype as function. If you use it you have to understand many things how jqGrid work internally. You should examine it's source code to be sure that you do all things correctly. If you skip somethings, than your code will works incorrect in some situations or in some combination of jqGrid parameters.

如果您看一下答案开头的代码(ajaxGridOptionsserializeGridData的用法),您会发现该代码非常简单.此外,它可以与jqGrid参数的所有其他合法组合一起使用.例如,您可以使用loadonce: trueloadCompleteloadError或什至虚拟滚动(scroll: 1scroll: true).所需的所有内容均取决于参数为您执行jqGrid .

If you look at the code which I included at the beginning of my answer (the usage of ajaxGridOptions and serializeGridData) you will see that the code is very easy. Moreover it works with all other legal combination of jqGrid parameters. For example you can use loadonce: true, loadComplete, loadError or even virtual scrolling (scroll: 1 or scroll: true). All things needed depend on the parameters do jqGrid for you.

这篇关于JQGrid:loadComplete在数据类型:函数时不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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