近日有個需求要用到jqgrid,原本用着一切都很順利,但是在需求變動后,只是修改部分字段名稱jqgrid就不顯示數據了,后台數據也能傳到前台,但是就是不給我顯示,到嘴的肉就是沒法吃,蛋疼,郁悶都無法形容我的感受。。。。。。我開始懷疑自個了
jQuery("#grid-table").jqGrid({ url:'<%=request.getContextPath()%>/salesAnalysis.do?method=getIntelligenceSaleTree', postData:getParam(),//參數 datatype: "json", jsonReader : { root:"data",//array或者List數據 page: "page",//當前頁碼 total: "pages",//總頁數 records: "count",//總記錄數 repeatitems: false }, colNames:['地區','城市','區縣','客戶名稱','期初庫存','購進數量','銷量數量','期末庫存'], colModel:[ {name:'PROV_NAME',index:'PROV_NAME', sorttype:'text'}, {name:'CITY_NAME',index:'CITY_NAME',}, {name:'AREA_NAME',index:'AREA_NAME'}, {name:'CLIENT_NAME',index:'CLIENT_NAME',summaryType:'count', summaryTpl : '{0}'}, {name:'BEGIN_STOCK',index:'BEGIN_STOCK', align:"right", sorttype:'number',formatter:'number',summaryType:'sum'}, {name:'CODE_PURCHASE_COUNT',index:'CODE_PURCHASE_COUNT', align:"right",sorttype:'number',formatter:'number',summaryType:'sum'}, {name:'CODE_SALE_COUNT',index:'CODE_SALE_COUNT', align:"right",sorttype:'number',formatter:'number', summaryType:'sum'}, {name:'END_STOCK',index:'END_STOCK',align:"right",sorttype:'number',formatter:'number', summaryType:'sum'} ], rowNum:10, rowList:[10,20,30], height: 'auto', autowidth: true, pager: '#gridpager', sortname: 'PROV_NAME', viewrecords: true, sortorder: "desc", caption:"智能銷售樹", grouping: true, groupingView : { groupField : ['PROV_NAME','CITY_NAME','AREA_NAME'], //分組字段 groupColumnShow : [true,false,false],//是否隱藏分組字段 groupText : ['<b>{0}</b>'], groupCollapse : false, groupOrder: ['asc'],// 分組后的排序 groupSummary : [true], groupDataSorted : true,// 分組中的數據是否排序 showSummaryOnHide : false //是否在分組底部顯示匯總信息並且當收起表格時是否隱藏下面的分組 }, footerrow: false, userDataOnFooter: false , gridComplete : function() { var ids = $("#grid-table").jqGrid("getDataIDs"); for(var i=0;i < ids.length;i++){ var cl = ids[i]; //置空 var city = ""; $("#grid-table").jqGrid("setRowData",cl,{PROV_NAME:city}); } }, loadComplete : replaceCss });
各種嘗試,包括變換請求方式,本地json,都是無法解決,郁悶之極,周末都在想這個問題,終於,周一早上Google了才發現jqgrid對json的數據格式校驗異常的嚴格,包括對單引號雙引號,jqgrid要求用雙引號,如果你用了單引號可得注意了,但是我發現的都是雙引號。
那我的問題出在哪兒呢?
我從firebug里把傳到前台的json復制出來放到在線的json檢測工具里(http://www.bejson.com/index.php),問題發現了,原來里面有個字段數據是"長清區\0\0",就多了\0\0 jqgrid就無法顯示了,OMG......我后台的數據都是我臨時添加的用於測試的,於是乎把數據庫中\0\0去掉就解決了,爽。。。。。。
總結:1,首先要對自個寫的代碼要有足夠的自信,這個在解決問題的時候要少走很多彎路。
2,要利用工具快速的縮小問題的范圍,如firebug就知道數據是正常傳遞的。
3,要對最大化地了解所用技術的脾氣。就比如我這個。當然參透api是最好的了。