使用jQuery在表中附加JSON数据

时间:2013-10-18 06:23:09

标签: jquery json

我有一张这样的表:

scan_time   credential  status      asset_code

value10     value12     value13     value14
value20     value22     value23     value24
value30     value23     value24     value25

我有一个像这样的JSON数据:

[
  {
    "scan_time": "Oct. 18, 2013, 6:27 p.m",
    "credential": "iexactlyknow",
    "status": 1,
    "asset_code": "you10"
  },
  {
    "scan_time": "Oct. 18, 2013, 6:20 p.m",
    "credential": "302E32325F302E30",
    "status": 2,
    "asset_code": "10001"
  }
]

我想在表格中附加JSON数据:

scan_time                   credential         status       asset_code

Oct. 18, 2013, 6:27 p.m     iexactlyknow          1         you10
Oct. 18, 2013, 6:20 p.m     302E32325F302E3       2         10001
value10                     value12            value13      value14
value20                     value22            value23      value24
value30                     value23            value24      value25

有可能吗?

如果难以追加2行,那么你可以考虑只追加1行, 然后你可以考虑这样的数据:

[
  {
    "scan_time": "Oct. 18, 2013, 6:20 p.m",
    "credential": "302E32325F302E30",
    "status": 2,
    "asset_code": "10001"
  }
]

1 个答案:

答案 0 :(得分:0)

您可以轻松完成,请参阅下面的代码。我没有输入所有列,只是循环你的json数据,基本上填充模板并采用生成的html到你的表。

var $table = $("table#mytable");
function addRowsFromData(data) { //data is an array of objects as specified - parse json first
    var rows = "";
    $.each(data, function(i, val) {
        rows += "<tr><td>" + val.scan_time + "</td><td>" + val.credential + "</td>" + /*the rest of your columns*/ ;
    });
    $table.prepend(rows);
}