获取错误:colNames的长度<> colModel或0!在jqgrid

时间:2016-02-12 10:42:40

标签: javascript jqgrid jqgrid-asp.net

我使用jQgrid显示数据库中的数据。

点击我的按钮时,我收到此错误:

length of colNmaes <>colModel or 0!

这是我的代码:

$(document).ready(function () {
    $('#btndist').click(function () {
        $.ajax({
            url:"default.aspx/loaddata",
            datatype:"json",
            data: "{}",
            contentType: "application/json;charset=utf-8",
            method:"POST",
            success: function (result) {
                result=result.d;
                jQuery("#Distable").jqGrid({
                    datatype: "local",
                    colModel: [
                        { name:"EmpID", index:"EmpID",width:80},
                        { name:"EmpFisrtName", index:"EmpFisrtName", width: 80 },
                        { name:"EmpLastName", index:"EmpLastName", width: 80 },
                        { name:"EmailAddress", index:"EmailAddress", width: 80 },
                        { name:"MobileNo", index:"MobileNo", width: 80 },
                        { name:"CityName", index:"CityName", width: 80 }
                    ],
                    data: JSON.parse(result),
                    rowNum: 10,
                    loadonce: true,                          
                    rowList: [5, 10, 20],
                    pager: '#DistPager',
                    viewrecords: true,
                    sortorder: 'asc',
                    gridview: true,
                    autowidth:true,
                    sortname: 'EmpName',
                    height:'auto',
                    altRows: true,
                    hoverrows: true,
                    caption:"List Employee Details"                          
                });
            },
            error: function (error) {
                alert("Oops an error");
            }
        });
    });
});

任何人都可以告诉我为什么会收到这个错误吗?

2 个答案:

答案 0 :(得分:0)

您必须添加colNames,如下所示。

colNames: ['Emp ID', 'Fisrt Name', 'Last Name', 'Email', 'Mobile No', 'City']

将显示为列标题。

更新:移除ajax来电,jqGrid可以从url本身加载数据。

$(document).ready(function() {
    $('#btndist').click(function() {
        jQuery("#Distable").jqGrid({
            url: "default.aspx/loaddata",
            datatype: "json",
            colNames: ['Emp ID', 'Fisrt Name', 'Last Name', 'Email', 'Mobile No', 'City'],
            colModel: [
                { name: "EmpID", index: "EmpID", width: 80 },
                { name: "EmpFisrtName", index: "EmpFisrtName", width: 80 },
                { name: "EmpLastName", index: "EmpLastName", width: 80 },
                { name: "EmailAddress", index: "EmailAddress", width: 80 },
                { name: "MobileNo", index: "MobileNo", width: 80 },
                { name: "CityName", index: "CityName", width: 80 }
            ],
            rowNum: 10,
            loadonce: true,
            rowList: [5, 10, 20],
            pager: '#DistPager',
            viewrecords: true,
            sortorder: 'asc',
            gridview: true,
            autowidth: true,
            sortname: 'EmpName',
            height: 'auto',
            altRows: true,
            hoverrows: true,
            caption: "List Employee Details"
        });
    });
});

答案 1 :(得分:0)

那是因为您尚未添加colNames

必须将 count colModel中的条目进行匹配。

如果列数与colModel中的条目数不相同,则主要提示以下错误。

所以您的情况应该是这样。

colNames: ['Emp ID', 'Emp First Name', 'Emp Last Name', 'Email Address', 'Mobile No', 'City Name'],



colModel: [
      { name: "EmpID", index: "EmpID", width: 80 },
      { name: "EmpFisrtName", index: "EmpFisrtName", width: 80 },
      { name: "EmpLastName", index: "EmpLastName", width: 80 },
      { name: "EmailAddress", index: "EmailAddress", width: 80 },
      { name: "MobileNo", index: "MobileNo", width: 80 },
      { name: "CityName", index: "CityName", width: 80 }
           ],