【发布时间】:2015-10-29 05:24:52
【问题描述】:
我想通过映射第一行(动态 - 产品)和列(供应商)来显示单元格的 JSON 数据。这是 JSON 输入和代码:
$(document).ready(function () {
var json = [{
"Supplier": "Supplier1",
"Product": "Oxygen",
"Quantity": 50
}, {
"Supplier": "Supplier1",
"Product": "Hydrogen",
"Quantity": 100
}, {
"Supplier": "Supplier1",
"Product": "Carbon",
"Quantity": 50
}, {
"Supplier": "Supplier2",
"Product": "Carbon",
"Quantity": 200
}, {
"Supplier": "Supplier2",
"Product": "Oxygen",
"Quantity": 100
}, {
"Supplier": "Supplier3",
"Product": "Hydrogen",
"Quantity": 50
}];
var tr;
for (var i = 0; i < json.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + json[i].Supplier + "</td>");
tr.append("<td>" + json[i].Product + "</td>");
tr.append("<td>" + json[i].Quantity + "</td>");
$('table').append(tr);
}
});
这是桌子:
我想重新排列数据来得到这个:
映射供应商和产品,显示数量。都是动态值。
请帮忙。 提前致谢。
【问题讨论】:
-
是否可以更改收到的 JSON 以更好地匹配表格格式?
-
@RoryMcCrossan 应该是哪种格式?
-
供应商数组,每个供应商都包含一系列产品。
-
@skubski 怎么绑定?
标签: javascript jquery html arrays json