我尝试了以下帖子中的建议,试图解决我的问题:
我有一个Infragistics igGrid,它使用javascript对象的javascript数组作为其数据源。当我将行硬编码到我的数组中时,网格可以完美地加载行。但是当我尝试通过AJAX调用从我的数据库中获取行时,网格不会加载任何行。这是我的代码:
<table id="grid"></table>
<script type="text/javascript">
$(function () {
// user list is initially empty
var userList = [];
// get user list
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8;",
url: "/AJAX_Handlers/GetUserList.ashx",
dataType: "json",
success: function (Result) {
// load the user list
$.each(Result, function (i, item) {
userList[i] = { "Name": Result[i]["Name"], "Email": Result[i]["Email"], "OPhone": Result[i]["OPhone"], "CPhone": Result[i]["CPhone"], "HPhone": Result[i]["HPhone"], "FPhone": Result[i]["FPhone"], "Address1": Result[i]["Address1"], "Address2": Result[i]["Address2"], "City": Result[i]["City"], "ZIP": Result[i]["ZIP"], "Active": Result[i]["Active"], "Contractor": Result[i]["Contractor"] };
});
},
failure: function (arg1, arg2) {
alert(arg1 + '\n\n' + arg2);
},
error: function (Result, Error, arg3, arg4) {
alert(Result + '\n\n' + Error + '\n\n' + arg3 + '\n\n' + arg4);
}
});
// define grid properties
$("#grid").igGrid({
columns: [
{ headerText: "Name", key: "Name", dataType: "string" },
{ headerText: "Email", key: "Email", dataType: "string" },
{ headerText: "Office #", key: "OPhone", dataType: "string" },
{ headerText: "Cell #", key: "CPhone", dataType: "string", hidden: "true" },
{ headerText: "Home #", key: "HPhone", dataType: "string", hidden: "true" },
{ headerText: "Fax #", key: "FPhone", dataType: "string", hidden: "true" },
{ headerText: "Address", key: "Address1", dataType: "string", hidden: "true" },
{ headerText: "Apt/Suite", key: "Address2", dataType: "string", hidden: "true" },
{ headerText: "City", key: "City", dataType: "string" },
{ headerText: "State", key: "StateName", dataType: "string" },
{ headerText: "ZIP", key: "ZIP", dataType: "string" },
{ headerText: "Active", key: "Active", dataType: "bool" },
{ headerText: "Contractor", key: "Contractor", dataType: "bool" }
],
width: "100%",
height: "400px",
autoGenerateColumns: false,
renderCheckboxes: true,
dataSource: userList
});
});
</script>
现在,在我定义igGrid之前添加以下行:
alert("hey");
网格会很好地加载行。我知道“GetUserList.ashx”中的代码工作正常,因为当我使用该警报框时网格正在工作。任何人都可以提出任何关于我可能在哪里出错的建议吗?
答案 0 :(得分:1)
事实证明我的网格初始化速度比我的AJAX调用发送它的响应要快,所以userList数组在网格初始化时没有行。为了解决这个问题,我简单地在我的AJAX函数的“success”事件中初始化了我的网格,以保证userList数组在网格初始化之前有数据。
以下是我从Infragistics获得的回复:
http://www.infragistics.com/community/forums/t/91953.aspx
他们提供了4条建议,第一条是帮助我的建议。
希望这能帮助别人,就像它帮助我一样!