请有人帮我解决这个问题。我正在尝试从名为simple8.php的php脚本添加动态列名和列模型信息,并且数据是从simple7.php获取的。
<script type="text/javascript">
jQuery(function($) {
var seq= $('#sim').text();
var seq= seq.replace(/[\“\”\″]/ig,'\"');
var seq = jQuery.trim(seq);
var title = $('#title_for_table').text();
var title = jQuery.trim(title);
$.ajax({
url:"simple8.php"+seq,
type: "POST",
data: "",
dataType: "json",
success: function (data) {
var ColN = data.colNames;
var ColM = data.colModel;
jQuery("#tableid").jqGrid({
url: "simple7.php"+seq,
datatype: "jsonstring",
mtype: "POST",
colNames: ColN,
colModel: ColM,
pager: '#pager',
rowNum:50,
rowList:[25,50,75,100],
sortname: 'r1',
sortorder: "asc",
viewrecords: true,
gridview: true,
height: '100%',
width: '100%',
caption:title+" Search Results",
loadComplete: function(reload) {
jQuery("#tableid").trigger("reloadGrid");
},
});
jQuery("#tableid").jqGrid('navGrid','#pager',{add:false,edit:false,del:false});
jQuery("#tableid").jqGrid('navButtonAdd','#pager',{caption: "Export",onClickButton: function () { //exportExcel($(this));
ExportJQGridDataToExcel($(this),'file1.xls');
},position:"last"
});
},
error: function () {
alert("Error with AJAX callback");
}
});
});
</script>
simple8.php的输出是:
{"colNames":"['col','r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9', 'r10', 'r11']","colModel":"[{name:'col', index:'col', width:120},{name:'r1', index:'r1', width:120},{name:'r2', index:'r2', width:120},{name:'r3', index:'r3', width:120},{name:'r4', index:'r4', width:120},{name:'r5', index:'r5', width:120},{name:'r6', index:'r6', width:120},{name:'r7', index:'r7', width:120},{name:'r8', index:'r8', width:120},{name:'r9', index:'r9', width:120},{name:'r10', index:'r10', width:120},{name:'r11', index:'r11', width:120}]"}
和simple7.php给出:
{"total":1,"page":1,"records":4,"rows":[{"id":null,"cell":["Chaffa","R","R","C","A","C","A","A","T","T","G","G"]},{"id":null,"cell":["CSG8962","R","R","Y","A","C","A","A","T","T","G","S"]},{"id":null,"cell":["DCP92_3","G","A","C","M","C","A","A","T","W","G","G"]},{"id":null,"cell":["DigVijay","R","A","C","A","C","A","A","T","T","G","S"]}]}
请告诉我错误的地方。
答案 0 :(得分:0)
colNames
的值是字符串而不是项目数组。同一个人可以说colModel
。您应该将从服务器返回的数据更改为仅使用"
而不是'
,然后使用colNames: $.parseJSON(ColN)
和colModel: $.parseJSON(ColM)
代替colNames: ColN, colModel: ColM
。如果不将'
替换为"
,则在$.parseJSON
中解析JSON时会出错。