JQGrid - 网格显示但数据未加载

时间:2015-09-06 16:08:44

标签: jquery jqgrid

请帮忙。我已经搜索了旧问题,但找不到相关的解决方案。

单击网格显示但没有数据加载的按钮。

我的网络回复:

{"d":"{"totalpages":2,"currpage":1,"totalrecords":15,"rows":[{"id":"110","cell":["110","perform action 1"]},{"id":"112","cell":["112","perform action 2"]},...]}"}

我的JQuery代码:

    $("#b4").click(function () {doAjax4();});

   function doAjax4() {
        $.ajax({
            async: false, cache: false,
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "WebAction.aspx/GetDataTable",
            data: "{}",
            datatype: "json",
            success: function (data) {
                $("#mygrid1").jqGrid({
                    dataType: "json",
                    type: "POST",
                    colNames: ['runid', 'action'],
                    colModel: [{ name: 'runid', index: 'runid' }, { name: 'action', index: 'action' }],
                    jsonReader: {
                        root: "rows",
                        page: "currpage",
                        total: "totalpages",
                        records: "totalrecords",
                        id: "id",
                        cell: "cell",
                        repeatitems: true
                    },
                    loadonce: true,
                    viewrecords: true,
                    gridview: true,
                    rowList: [5, 10, 50],
                    caption: "Action Table",
                    height: 'auto',
                    //pager: '#pager',
                    emptyrecords: 'No data for the applied filter'
                });
            },
            error: function (xhr, type, exception) {
                alert(xhr.statusText);
            }
        })
    }

1 个答案:

答案 0 :(得分:0)

我在您的代码中发现了许多问题:

  • 服务器响应的d属性中的值类型为 string 。因此,我认为您在Web服务GetDataTable中出现了典型的初学者错误:您在代码中直接包含了不需要的转换为JSON。正确的是从GetDataTable返回对象而不是字符串
  • 您在datatype: "json"中使用$.ajax参数,但您应该使用dataType: "json"。 jqGrid中关闭选项的名称为datatype,但$.ajax中的名称为dataType
  • 您使用了我建议删除的不需要的参数async: false
  • 您在doAjax4处理程序内部调用click。函数doAjax4通过执行代码success ...}}; $("#mygrid1").jqGrid({ . It's important to understand, that jqGrid convert initial empty table ( $(“#mygrid1”)在) to relatively complex structure of divs and tables. Thus **you can't create the same grid more as once**. The second coll will be just ignored. To fix the problem you should include the test whether the grid is already created of not and use处理程序内创建网格.trigger(“reloadGrid”); instead of $(“#mygrid1”)。jqGrid({...});。解决问题的另一种方法是调用GridUnload方法之前 $("#mygrid1").jqGrid({ ...});`。如果现有网格存在,它将销毁它。
  • jqGrid的选项包含dataType: "json"参数而不是datatype: "json",因此jqGrid使用默认选项datatype: "xml",这可能不是您想要的。以同样的方式使用type: "POST"中存在但未在jqGrid中存在的未知选项$.ajax。您应该将选项名称从type修改为mtype。如果您希望jqGrid发出HTTP POST请求,那么您应该指定url参数(例如url: "WebAction.aspx/GetDataTable"
  • 第一个Ajax调用中返回的data将被忽略。如果您希望jqGrid使用之前下载的data,那么您应该使用例如datatype: "jsonstring", datastr: data.d。它将与您当前的数据格式相对应。
相关问题