[英]How do I display JSON file data into HTML table (using JavaScript only, not jQuery)
[英]Display data in an HTML table using JavaScript/jQuery
我有一個JSON響應,如下所示。
"result": [
[1, 0.10, 1.00],
[2, 0.20, 2.00],
[3, 0.30, 3.00],
[4, 0.40, 4.00],
[5, 0.50, 5.00],
[6, 0.60, 6.00],
[7, 0.70, 7.00],
[8, 0.80, 8.00],
[9, 0.90, 9.00],
[10, 1.00, 10.00],
[11, 1.10, 11.00],
[12, 1.20, 12.00],
[13, 1.30, 13.00],
[14, 1.40, 14.00],
[15, 1.50, 15.00],
[16, 1.60, 16.00],
[17, 1.70, 17.00],
[18, 1.80, 18.00]
]
這對應於Java中的java.util.List<Object[]>
。
響應由以下JavaScript / jQuery函數接收。
var timeout;
var request;
$(function () {
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
$(document).ajaxSend(function(e, xhr, options) {
xhr.setRequestHeader(header, token);
});
});
$(document).ready(function(){
$("#zoneCharge").change(function(){
if(!request)
{
request = $.ajax({
datatype:"json",
type: "POST",
data: JSON.stringify({jsonrpc:'2.0', method:'getZoneCharges', id:'jsonrpc', params:[$("#zoneCharge").val()]}),
contentType: "application/json-rpc; charset=utf-8",
url: "ZoneChargeList",
success: function(response)
{
var list=response.result;
var tempWeight='';
$('#dataTable').remove();
var $table = $("<table id='dataTable' cellpadding='0' cellspacing='0' width='100%'>").appendTo($('#zoneChargeList'));
$('<tr>').appendTo($table)
//.append($("<th style='width: 96px;'>").text("Weight"))
//.append($("<th style='width: 96px;'>").text("Charge"))
.append($("<th style='width: 96px;'>").text("Weight"))
.append($("<th style='width: 96px;'>").text("Charge"));
$.each(list, function(index, list) {
list[2]===null||list[2]===undefined||list[2]===''||isNaN(list[2])?tempWeight='':tempWeight=list[2].toFixed(2);
$('<tr>').appendTo($table)
.append($('<td>').text(list[1]))
.append($("<td><input type='text' name='txtCharge[]' value='"+tempWeight+"' onkeypress='return isNumberKey(event, this.value);'>"));
});
},
complete: function()
{
timeout = request = null;
},
error: function(request, status, error)
{
if(status!=="timeout"&&status!=="abort")
{
alert(status+" : "+error);
}
}
});
timeout = setTimeout(function() {
if(request)
{
request.abort();
alert("The request has been timed out.");
}
}, 30000);
}
});
});
<span id="zoneChargeList"></span>
我想以下列格式在<table>
中顯示此數據列表。
成功處理程序中的$.each
函數當前以下列格式在<table>
中顯示此列表。
如何在四列表中顯示此列表,如上面的等效快照所示?
當從<select>
一個id
為zoneCharge
的項時,將調用jQuery函數
這樣的事情應該有用(這里有一個小提琴演示: http : //jsfiddle.net/83d4w/5/ ):
var newTr = null;
var resetTr = true;
$.each(list, function(index, list) {
list[2]===null||list[2]===undefined||list[2]===''||isNaN(list[2])?tempWeight='':tempWeight=list[2].toFixed(2);
if (!newTr) {
// create new row
newTr = $('<tr>');
// do not reset it this time
resetTr = false;
} else {
// we used the previous one so reset it this time
resetTr = true;
}
newTr.appendTo($table)
.append($('<td>').text(list[1]))
.append($("<td><input type='text' name='txtCharge[]' value='"+tempWeight+"' onkeypress='return isNumberKey(event, this.value);'>"));
if (resetTr) {
// reset
newTr = null;
}
});
newTr
包含當前使用的tr
或為null。 resetTr
將決定我們newTr
在循環結束時重置newTr
。 [編輯]哦,當然你可以取消注釋兩個標題單元格
您必須用實際的for
循環替換$.each
調用,然后在循環內部為index
和index+1
創建td
標記。 然后for
循環應該將索引增加2而不是1.這樣,你的循環基本上為每個其他索引添加一行,並且每行放置兩個索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.