使用动态列和模型在 AJAX 成功时加载 JQGRID
Load JQGRID on AJAX success with dynamic columns and model
我在这方面做了足够多的研究,但仍然是个谜。
我从服务器中提取 table 架构以创建工作正常的列 (result.colNames
) 和模型 (result.colModel
)。
结果相同,我正在拉取 table 数据(result.colD
)以成功填充到 JQgrid 中。
成功时网格创建良好,但数据未加载到其中。
这里是代码和截图。
我花了很多时间在这上面,然后在这里发帖..希望这能在这里得到解决。
$.ajax({
type: "GET",
url: "webapi/do/pullSchema/"+display,
data: "",
dataType: "json",
success: function(result)
{
colD = JSON.stringify(result.colData);
colN = result.colNames;
colM = result.colModel;
jQuery("#list").jqGrid({
data:JSON.parse(colD),datatype: "local",
colNames:colN, colModel :colM,
pager: jQuery('#pager'),
rowNum: 5,
rowList: [5, 10, 20, 50],
viewrecords: true,
caption: 'DAta from table',
loadtext:'Loading, please wait'});
},
error: function(x, e)
{
alert(x.readyState + " "+ x.status +" "+ e.msg);
}
});
下面是示例数据集:
[dbname=null, tables=null, ColNames=[Plan_code, LOB],
colModel=[{name:'Plan_code',index:'Plan_code',width:255}, {name:'LOB',index:'LOB',width:255}], colData=[{LOB=N, Plan_code=C82ACC}, {LOB=P, Plan_code=C82ACC}, {LOB=B, Plan_code=C82ACC}, {LOB=I, Plan_code=C82ACC}, {LOB=I, Plan_code=C82IRA}, {LOB=R, Plan_code=C82IRA}]
问题是 colData 不是 jqgrid 期望的格式 json(这是许多 jqgrid 开发人员常犯的错误)
有关详细信息,请参阅 中的问题。
因此,在您的情况下,解决方法是操纵 colData 并创建正确的 json。
colRows=[{LOB=N, Plan_code=C82ACC}, {LOB=P, Plan_code=C82ACC}, {LOB=B, Plan_code=C82ACC}, {LOB=I, Plan_code=C82ACC}, {LOB=I, Plan_code=C82IRA}, {LOB=R, Plan_code=C82IRA}]
colData = {
"total": 1,
"page": 1,
"records": colRows.count,
rows:[{LOB=N, Plan_code=C82ACC}, {LOB=P, Plan_code=C82ACC}, {LOB=B, Plan_code=C82ACC}, {LOB=I, Plan_code=C82ACC}, {LOB=I, Plan_code=C82IRA}, {LOB=R, Plan_code=C82IRA}]
}
Send this to jqgrid and then it will know how to display it.
问题已解决:
使用以下函数创建动态列 header 和列模型。函数 getColNames(数据){
变种键= [];
对于(数据中的变量键){
如果(data.hasOwnProperty(键)){
keys.push(关键);
}
}
return keys;
}
function getColModels(data) {
var colNames= getColNames(data);
var colModelsArray = [];
for (var i = 0; i < colNames.length; i++) {
var str;
if (i === 0) {
str = {
name: colNames[i],
index:colNames[i],
key:true,
editable:true
};
} else {
str = {
name: colNames[i],
index:colNames[i],
editable:true
};
}
colModelsArray.push(str);
}
return colModelsArray;
}
我在这方面做了足够多的研究,但仍然是个谜。
我从服务器中提取 table 架构以创建工作正常的列 (result.colNames
) 和模型 (result.colModel
)。
结果相同,我正在拉取 table 数据(result.colD
)以成功填充到 JQgrid 中。
成功时网格创建良好,但数据未加载到其中。
这里是代码和截图。
我花了很多时间在这上面,然后在这里发帖..希望这能在这里得到解决。
$.ajax({
type: "GET",
url: "webapi/do/pullSchema/"+display,
data: "",
dataType: "json",
success: function(result)
{
colD = JSON.stringify(result.colData);
colN = result.colNames;
colM = result.colModel;
jQuery("#list").jqGrid({
data:JSON.parse(colD),datatype: "local",
colNames:colN, colModel :colM,
pager: jQuery('#pager'),
rowNum: 5,
rowList: [5, 10, 20, 50],
viewrecords: true,
caption: 'DAta from table',
loadtext:'Loading, please wait'});
},
error: function(x, e)
{
alert(x.readyState + " "+ x.status +" "+ e.msg);
}
});
下面是示例数据集:
[dbname=null, tables=null, ColNames=[Plan_code, LOB],
colModel=[{name:'Plan_code',index:'Plan_code',width:255}, {name:'LOB',index:'LOB',width:255}], colData=[{LOB=N, Plan_code=C82ACC}, {LOB=P, Plan_code=C82ACC}, {LOB=B, Plan_code=C82ACC}, {LOB=I, Plan_code=C82ACC}, {LOB=I, Plan_code=C82IRA}, {LOB=R, Plan_code=C82IRA}]
问题是 colData 不是 jqgrid 期望的格式 json(这是许多 jqgrid 开发人员常犯的错误)
有关详细信息,请参阅
因此,在您的情况下,解决方法是操纵 colData 并创建正确的 json。
colRows=[{LOB=N, Plan_code=C82ACC}, {LOB=P, Plan_code=C82ACC}, {LOB=B, Plan_code=C82ACC}, {LOB=I, Plan_code=C82ACC}, {LOB=I, Plan_code=C82IRA}, {LOB=R, Plan_code=C82IRA}]
colData = {
"total": 1,
"page": 1,
"records": colRows.count,
rows:[{LOB=N, Plan_code=C82ACC}, {LOB=P, Plan_code=C82ACC}, {LOB=B, Plan_code=C82ACC}, {LOB=I, Plan_code=C82ACC}, {LOB=I, Plan_code=C82IRA}, {LOB=R, Plan_code=C82IRA}]
}
Send this to jqgrid and then it will know how to display it.
问题已解决:
使用以下函数创建动态列 header 和列模型。函数 getColNames(数据){ 变种键= []; 对于(数据中的变量键){ 如果(data.hasOwnProperty(键)){ keys.push(关键); } }
return keys;
}
function getColModels(data) {
var colNames= getColNames(data);
var colModelsArray = [];
for (var i = 0; i < colNames.length; i++) {
var str;
if (i === 0) {
str = {
name: colNames[i],
index:colNames[i],
key:true,
editable:true
};
} else {
str = {
name: colNames[i],
index:colNames[i],
editable:true
};
}
colModelsArray.push(str);
}
return colModelsArray;
}