1
我正在創建jqgrid,但json加載顯示的區域未定義。json數據不顯示在jqgrid
我無法弄清爲什麼數據顯示爲未定義。 下面是我的代碼:
$(document).ready(function(){
$.ajax({
type : "POST",
url : "../ShowUploadedCSVFile",
success : function(response) {
showJQgrid(response);
},
error : function() {
alert("Failure");
}
});
function showJQgrid(Data)
{
$("#showCSVList").jqGrid("clearGridData");
var grid = $('#showCSVList');
if (grid[0].grid == undefined)
{
}
else
{
delete grid;
$('#showCSVList').GridUnload('#showCSVList');
}
var parse = Data;
global = parse;
var Jsj = eval('(' + parse + ')');
var colNames = new Array();
var colModel = new Array();
var j = 0;
for (var i = 0; i < Jsj.Header.length; i++)
{
colNames.push(Jsj.Header[i]);
colModel.push({
name : Jsj.Header[i],
index : Jsj.Header[i],
width : '75%',
align : 'left',
editable : true
});
}
$('#showCSVList').jqGrid({
datatype : 'local',
colNames : colNames,
colModel : colModel,
scrollOffset : 0,
pager : '#showCSVPager',
rowNum : 5,
loadonce : true,
multiselect : true,
rowList : [ 5, 10, 20, 50 ],
viewrecords : true
});
for (var i = 0; i < Jsj.data.length; i++)
{
jQuery("#showCSVList").jqGrid('addRowData', i+1, Jsj.data[i]);
}
$("#showCSVList").jqGrid('setGridHeight', $("#center").height());
$("#showCSVList").navGrid("#showCSVPager", {
edit : false,
add : false,
del : true
});
$("#showCSVList").jqGrid('inlineNav', "#showCSVPager");
}
});
我的JSON文件是:
{
"Header": ["Name", "Party", "Province", "Age", "Gender"],
"data": [{
"Name": "Mourani, Maria",
"Age": "43",
"Gender": "Female",
"Party": "BlocQuebecois",
"Province": "Quebec"
}, {
"Name": "Sellah,Djaouida",
"Age": "30",
"Gender": "Female",
"Party": "NDP",
"Province": "Quebec"
}, {
"Name": "St-Denis,Lise",
"Age": "72",
"Gender": "Female",
"Party": "NDP",
"Province": "Quebec"
}]
}
這樣產生的輸出:
感謝@jammykam但我得到的錯誤,因爲我通過我的Ajax響應我的showJqGrid (Data)我得到的錯誤TypeError:Jsj.Header is undefined \t for(var i = 0; i
Mayur
@Mayur嘗試添加'dataType:「json」'屬性到您的$ .ajax請求強制MIME類型爲json:http://api.jquery.com/jquery.ajax/ – jammykam
我已經嘗試過但未能正常工作 – Mayur