JQGrid:loadComplete NOT数据类型:function时触发

0 jqgrid

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

Ole*_*leg 5

在上一个问题中,您写道您在服务器端使用WCF.在这种情况下,您不需要使用datatype作为功​​能.而不是你可以使用以下参数:

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

为了确保JSON.stringify在旧的Web浏览器中受支持,您应该包括json2.js可以从此处加载的内容.

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

现在我将回答你原来的问题:loadComplete"如果你使用datatype函数,为什么不激活" .简短的回答是:如果你使用datatype函数,你的代码负责调用loadComplete.

如果你使用datatypeas作为函数,你的代码对jqGrid通常做的一些事情负责.首先,你必须了解该datatype功能应该做什么.文档中的一个示例(参见此处)显示了datatype函数的最简单但不完整的实现.更完整的代码示例如下所示:

$("#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
});
Run Code Online (Sandbox Code Playgroud)

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

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

如果你看一下我在包括我的答案(的使用开始时的代码ajaxGridOptionsserializeGridData),您将看到该代码是很容易的.此外,它适用于jqGrid参数的所有其他合法组合.例如,你可以使用loadonce: true,loadComplete,loadError甚至是虚拟滚动(scroll: 1scroll: true).所需的一切都取决于jqGrid的参数.