【问题标题】:JQ Grid is not loading into Visual Studio 2013JQ Grid 未加载到 Visual Studio 2013
【发布时间】:2016-04-21 06:30:21
【问题描述】:

JQ Grid 没有加载到 Visual Studio 2013,但它在 Visual Studio 2010 中运行良好。当我在浏览器中按 F12 时出现以下错误:

 Uncaught TypeError: Cannot read property 'length' of undefined

以下脚本适用于 Visual Studio 2010 和 2008。

下面是我的 HTML 代码:

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
<title></title>

<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<link href="JQGridReq/jquery-ui-1.9.2.custom.css" rel="stylesheet" type="text/css" />
<link href="JQGridReq/ui.jqgrid.css" rel="stylesheet" type="text/css" />
  <script src="JQGridReq/jquery-1.9.0.min.js" type="text/javascript"></script>
<script src="JQGridReq/jquery.jqGrid.js" type="text/javascript"></script>

<script src="JQGridReq/grid.locale-en.js" type="text/javascript"></script>


<script type="text/javascript">

    $(function () {
        $("#dataGrid").jqGrid({
            url: '<%=ResolveUrl("Default5.aspx/GetDataFromDB") %>',
            datatype: 'json',
            mtype: 'POST',
            serializeGridData: function (postData) {
                return JSON.stringify(postData);
            },

            ajaxGridOptions: { contentType: "application/json" },
            loadonce: true,
            //toolbar: [true, "top"],
            colNames: ['Emp_ID', 'Name', 'Designation', 'City', 'State', 'Country'],
            colModel: [
                       { name: 'Emp_ID', index: 'Emp_ID', width: 150, stype: 'text' },
                       { name: 'Name', index: 'Name', width: 150, stype: 'text', sortable: true, editable: true },
                       { name: 'Designation', index: 'Designation', width: 150, editable: true },
                       { name: 'City', index: 'City', width: 150, editable: true },
                       { name: 'State', index: 'State', width: 150, align: "right", editable: true },
                       { name: 'Country', index: 'Country', width: 150, align: "right", editable: true },

            ],

            multiselect: true,
            multipleSearch: true,
            pager: '#pager',
            rowNum: 10,
            rowList: [10, 20, 30],
            viewrecords: true,
            gridview: true,
            jsonReader: {
                page: function (obj) { return 1; },
                total: function (obj) { return 1; },
                records: function (obj) { return obj.d.length; },
                root: function (obj) { return obj.d; },
                repeatitems: false,
                id: "0"
            },
            caption: 'Employee details'
        }).navGrid('#pager', { search: true, edit: false, add: false, del: false, searchtext: "Search" });
        jQuery("#dataGrid").jqGrid('navGrid', "#pager", {}, {}, {}, {},
     {
           multipleSearch: true, closeAfterSearch: true, closeAfterReset: true,
   onClose: function () {
       //do work
       return true; // return true to close the search grid
   }
   });
    })
</script>
 </head>
 <body>
<form id="form1" runat="server">
    <div>
        <table style="">
            <tr>
                <td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue; font-family: 'Times New Roman'; font-weight: bold; font-size: 20pt; color: chocolate;"></td>
            </tr>
            <tr>
                <td style="text-align: center; vertical-align: central; padding: 50px;">


                    <table id="dataGrid" style="text-align: center;">          </table>
                    <div id="pager"></div>
                </td>
            </tr>
        </table>
    </div>
</form>

【问题讨论】:

  • jquery-1.9.0.min.js文件放在jqGrid.js之前
  • 我的地方,但又是一样的: .@KartikeyaKhosla

标签: javascript c# jquery visual-studio-2013 jqgrid-asp.net


【解决方案1】:

您正在获取未定义的 e.i. 的长度。 obj.d 在记录中。您可以为此设置一个 if 条件。

if(obj.d !== undefined){ return obj.d.length; } else return 0;

【讨论】:

  • 如果条件我必须放在哪里,你能根据上面的更新整个答案。 @saddam_msp
  • 将此行替换为我的行:return obj.d.length;在记录:函数(obj){返回obj.d.length; },
  • 我是 Java Script 新手,请更新完整答案。 @saddam_msp
【解决方案2】:

替换这个。

<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<link href="JQGridReq/jquery-ui-1.9.2.custom.css" rel="stylesheet" type="text/css" />
<link href="JQGridReq/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script src="JQGridReq/jquery.jqGrid.js" type="text/javascript"></script>
<script src="JQGridReq/jquery-1.9.0.min.js" type="text/javascript"></script>
<script src="JQGridReq/grid.locale-en.js" type="text/javascript"></script>


<script type="text/javascript">

    $(function () {
        $("#dataGrid").jqGrid({
            url: '<%=ResolveUrl("Default5.aspx/GetDataFromDB") %>',
            datatype: 'json',
            mtype: 'POST',
            serializeGridData: function (postData) {
                return JSON.stringify(postData);
            },

            ajaxGridOptions: { contentType: "application/json" },
            loadonce: true,
            //toolbar: [true, "top"],
            colNames: ['Emp_ID', 'Name', 'Designation', 'City', 'State', 'Country'],
            colModel: [
                       { name: 'Emp_ID', index: 'Emp_ID', width: 150, stype: 'text' },
                       { name: 'Name', index: 'Name', width: 150, stype: 'text', sortable: true, editable: true },
                       { name: 'Designation', index: 'Designation', width: 150, editable: true },
                       { name: 'City', index: 'City', width: 150, editable: true },
                       { name: 'State', index: 'State', width: 150, align: "right", editable: true },
                       { name: 'Country', index: 'Country', width: 150, align: "right", editable: true },

            ],

            multiselect: true,
            multipleSearch: true,
            pager: '#pager',
            rowNum: 10,
            rowList: [10, 20, 30],
            viewrecords: true,
            gridview: true,
            jsonReader: {
                page: function (obj) { return 1; },
                total: function (obj) { return 1; },
                records: function (obj) { if(obj.d !== undefined){ return obj.d.length; } else return 0; },
                root: function (obj) { return obj.d; },
                repeatitems: false,
                id: "0"
            },
            caption: 'Employee details'
        }).navGrid('#pager', { search: true, edit: false, add: false, del: false, searchtext: "Search" });
        jQuery("#dataGrid").jqGrid('navGrid', "#pager", {}, {}, {}, {},
     {
           multipleSearch: true, closeAfterSearch: true, closeAfterReset: true,
   onClose: function () {
       //do work
       return true; // return true to close the search grid
   }
   });
    })
</script>
 </head>
 <body>
<form id="form1" runat="server">
    <div>
        <table style="">
            <tr>
                <td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue; font-family: 'Times New Roman'; font-weight: bold; font-size: 20pt; color: chocolate;"></td>
            </tr>
            <tr>
                <td style="text-align: center; vertical-align: central; padding: 50px;">


                    <table id="dataGrid" style="text-align: center;">          </table>
                    <div id="pager"></div>
                </td>
            </tr>
        </table>
    </div>
</form>

【讨论】:

  • 现在浏览器中未显示错误,但网格显示为空。 @saddam_msp
  • 因为你在 obj.d 中没有数据。检查它可能在 obj 中,替换根:function (obj) { return obj.d; },与根一致:function (obj) { return obj; },
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多