jQuery的:
var data = JSON.parse(response);
$.each(data, function(i) {
console.log(data[i].Serial);
table.row.add([{
".Serial": data[i].Serial,
".Purchase_id": data[i].Purchase_id,
".Item": data[i].Item,
".HSN": data[i].HSN,
".Unit": data[i].Unit,
".Quantity": data[i].Quantity,
".Purchase_rate": data[i].Purchase_rate,
".Discount": data[i].Discount,
".Discount_2": data[i].Discount_2,
".Net_rate": data[i].Net_rate,
".CGST_Percentage": data[i].CGST_Percentage,
".SGST_Percentage": data[i].SGST_Percentage,
".IGST_Percentage": data[i].IGST_Percentage,
".Rate_after_tax": data[i].Rate_after_tax,
".CGST": data[i].CGST,
".SGST": data[i].SGST,
".IGST": data[i].IGST,
".Net_amount_without_tax": data[i].Net_amount_without_tax,
".Net_amount": data[i].Net_amount,
".ID": data[i].ID
}]).draw();
});
回复数据:
[
{
"ID":"35",
"Serial":"1",
"Purchase_id":"10",
"Item":"famous sticker",
"Unit":"Piece",
"HSN":"84022020",
"Quantity":"10000",
"Purchase_rate":"40",
"Discount":"0",
"Discount_2":"0",
"Net_rate":"40",
"CGST_Percentage":"0",
"SGST_Percentage":"0",
"IGST_Percentage":"12",
"Rate_after_tax":"44.8",
"CGST":"0",
"SGST":"0",
"IGST":"48000",
"Net_amount_without_tax":"400000",
"Net_amount":"448000",
"Item_id":"27"
},
{
"ID":"36",
"Serial":"1",
"Purchase_id":"11",
"Item":"famous sticker",
"Unit":"Piece",
"HSN":"84022020",
"Quantity":"1",
"Purchase_rate":"10",
"Discount":"10",
"Discount_2":"0",
"Net_rate":"9",
"CGST_Percentage":"6",
"SGST_Percentage":"6",
"IGST_Percentage":"0",
"Rate_after_tax":"10.08",
"CGST":"0.54",
"SGST":"0.54",
"IGST":"0",
"Net_amount_without_tax":"9",
"Net_amount":"10.08",
"Item_id":"27"
}
]
HTML:
<tbody>
<tr>
<td class="Serial"></td>
<td class="Purchase_id"></td>
<td class="Item"></td>
<td class="HSN"></td>
<td class="Unit"></td>
<td class="Quantity"></td>
<td class="Purchase_rate"></td>
<td class="Discount"></td>
<td class="Discount_2"></td>
<td class="Net_rate"></td>
<td class="CGST_Percentage"></td>
<td class="SGST_Percentage"></td>
<td class="IGST_Percentage"></td>
<td class="Rate_after_tax"></td>
<td class="CGST"></td>
<td class="SGST"></td>
<td class="IGST"></td>
<td class="Net_amount_without_tax"></td>
<td class="Net_amount"></td>
<td class="ID"></td>
</tr>
</tbody>
任何人都可以告诉我为什么不将数据设置到表格?我想分别在td类上设置数据,但它不起作用。当我在控制台中显示数据中的数据然后它工作,但当我向表添加数据时,它不起作用。我该如何解决这个问题?抱歉我的英语不好。请编辑问题,以便此问题可以帮助其他人。
答案 0 :(得分:0)
尝试没有包含数据的{}也应该更容易使用
$.each(data, function(i,item) {
table.row.add([
".Serial": item.Serial,
".Purchase_id": item.Purchase_id,
".Item": item.Item,
".HSN": item.HSN,
".Unit": item.Unit,
".Quantity": item.Quantity,
".Purchase_rate": item.Purchase_rate,
".Discount": item.Discount,
".Discount_2": item.Discount_2,
".Net_rate": item.Net_rate,
".CGST_Percentage": item.CGST_Percentage,
".SGST_Percentage": item.SGST_Percentage,
".IGST_Percentage": item.IGST_Percentage,
".Rate_after_tax": item.Rate_after_tax,
".CGST": item.CGST,
".SGST": item.SGST,
".IGST": item.IGST,
".Net_amount_without_tax": item.Net_amount_without_tax,
".Net_amount": item.Net_amount,
".ID": item.ID
]).draw(); });
答案 1 :(得分:0)
可能你会考虑使用jquery append方法来创建如下所示的表。它消除了为每个表数据td
var response = [
{
"ID":"35",
"Serial":"1",
"Purchase_id":"10",
"Item":"famous sticker",
"Unit":"Piece",
"HSN":"84022020",
"Quantity":"10000",
"Purchase_rate":"40",
"Discount":"0",
"Discount_2":"0",
"Net_rate":"40",
"CGST_Percentage":"0",
"SGST_Percentage":"0",
"IGST_Percentage":"12",
"Rate_after_tax":"44.8",
"CGST":"0",
"SGST":"0",
"IGST":"48000",
"Net_amount_without_tax":"400000",
"Net_amount":"448000",
"Item_id":"27"
},
{
"ID":"36",
"Serial":"1",
"Purchase_id":"11",
"Item":"famous sticker",
"Unit":"Piece",
"HSN":"84022020",
"Quantity":"1",
"Purchase_rate":"10",
"Discount":"10",
"Discount_2":"0",
"Net_rate":"9",
"CGST_Percentage":"6",
"SGST_Percentage":"6",
"IGST_Percentage":"0",
"Rate_after_tax":"10.08",
"CGST":"0.54",
"SGST":"0.54",
"IGST":"0",
"Net_amount_without_tax":"9",
"Net_amount":"10.08",
"Item_id":"27"
}
]
;
var table = $("table tbody");
var data = response;
$.each(data, function(i) {
console.log(data[i].Serial);
var d = data[i];
var tr = $("<tr></tr>");
for(var e in d){
tr.append("<td>"+d[e]+"</td>");
}
table.append(tr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<table border="1">
<tbody>
</tbody>
</table>
</body>