JQGrid:当数据类型:函数时加载完成不触发 [英] JQGrid: loadComplete NOT firing when datatype: function

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

问题描述

如果我调用一个函数来加载我的网格数据,loadComplete 不会触发.我需要处理此事件,以便我可以正确手动更新多选复选框.如果我在 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函数应该做什么.文档中的一个示例(参见 here)显示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: 1 或 <代码>滚动:真).所有需要的东西都取决于参数为你做 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:当数据类型:函数时加载完成不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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