admin管理员组文章数量:1391977
I have a JSON response like the following.
"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]
]
This corresponds to a java.util.List<Object[]>
in Java.
The response is received by the following JavaScript/jQuery function.
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);'>"));
});
},
plete: 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>
I want to display this list of data in <table>
in the following format.
The $.each
function in the success handler currently displays this list in <table>
in the following format.
How to show this list in a table of four columns as shown by an equivalent snap shot above?
The jQuery function is invoked when an item is selected from <select>
whose id
is zoneCharge
I have a JSON response like the following.
"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]
]
This corresponds to a java.util.List<Object[]>
in Java.
The response is received by the following JavaScript/jQuery function.
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);'>"));
});
},
plete: 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>
I want to display this list of data in <table>
in the following format.
The $.each
function in the success handler currently displays this list in <table>
in the following format.
How to show this list in a table of four columns as shown by an equivalent snap shot above?
The jQuery function is invoked when an item is selected from <select>
whose id
is zoneCharge
- Honestly, you could probably save yourself a lot of bother with something along the lines of datatables – Moby's Stunt Double Commented Feb 18, 2014 at 19:50
-
not related, but...
datatype
->dataType
– Kevin B Commented Feb 18, 2014 at 19:51 -
I have changed it to
dataType
, @KevinB. Thanks. – Tiny Commented Feb 18, 2014 at 21:03
2 Answers
Reset to default 2Something like this should work (and here's a fiddle to demonstrate: http://jsfiddle/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
contains the currenttr
being used or is null.resetTr
will decide if we resetnewTr
at the end of the loop.
[edit] Oh and of course you can unment the two heading cells
You'll have to replace your $.each
call with an actual for
loop, and then inside your loop, create the td
tags for index
and index+1
. Then the for
loop should increment the index by 2 instead of by 1. That way, your loop is basically adding a row for every other index, and putting two indices per row.
本文标签: Display data in an HTML table using JavaScriptjQueryStack Overflow
版权声明:本文标题:Display data in an HTML table using JavaScriptjQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744706805a2620887.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论