json資料 填充遍歷表格行列 jQuery
阿新 • • 發佈:2019-01-10
var year=${year}; function dataFill(data){ var month = 1; $("table tr").each(function(){ var fid ="" ; if($(this).children("td:nth-child(2)").attr("id")!=undefined){ fid= $(this).children("td:nth-child(2)").attr("id")+"Qms"; var res = 0.0;//資料庫中某欄位的結果 for(month=1;month<=12;month++ ){ var col = month+3; if(data[month]==null){ }else{ res = data[month][fid]; var result = ""; if(res==null){result="";} else { result = res+""; } $(this).children("td:nth-child("+col+")").text(result); } } } }); } function show(){ jsTool.ajax({ url:'${contextPath}/show/showZcfzb2.do', data:{'year':year}, success:function(data){ if(data!=null){ jsondata = data; var i=6; alert(data[i].fzhsyzqyhgdqyhjQms); dataFill(data); } } }); } jQuery(document).ready( function() { show(); alert("show"); } );
</pre><pre code_snippet_id="432070" snippet_file_name="blog_20140721_3_1958972" name="code" class="javascript">
二號方案 後臺直接return一個List jackson將之轉換為 [{,,,},{,,,}] 類似形式
var year=${year}; function dataFill(data){ var month = 1; $("table tr").each(function(){ var fid ="" ; if($(this).children("td:nth-child(2)").attr("id")!=undefined){ fid= $(this).children("td:nth-child(2)").attr("id")+"Qms"; var res = 0.0;//資料庫中某欄位的結果 var col = 3;//期初餘額 if(data[month]==null){ }else{ month = 1; fid = $(this).children("td:nth-child(2)").attr("id")+"Qcs"; for ( var o in data) {//從json裡遍歷判斷month if(data[o].month == month){ res = data[o][fid]; } } var result = ""; if(res==null){result="";} else { result = res+""; } $(this).children("td:nth-child("+col+")").text(result); } for(month=1;month<=12;month++ ){//讀取1到12月的資料 (一行) var col = month+3; for ( var o in data) {//從json裡遍歷判斷month if(data[o].month == month){ result = data[o][fid]; } } if(result==null){result=""} $(this).children("td:nth-child("+col+")").text(result); } } } ); } function show(){ jsTool.ajax({ url:'${contextPath}/show/showZcfzb2.do', data:{'year':year}, success:function(data){ if(data!=null){ dataFill(data); loadingHide(); } } }); } jQuery(document).ready( function() { loading(); show(); } );