最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to appendChild an array of object in a table? - Stack Overflow

programmeradmin1浏览0评论

I have an array of objects, that I would like to add to my table.

I am failing to understand how can I append this data to a table.

    var data = [{
      id: 12,
      data: 70,
      mama: "sjk"
    }, {
      id: 12,
      data: "mou",
      mama: "sjk"
    }, {
      id: 12,
      data: "mou",
      mama: "sjk"
    }]

    for (var i = 0; i < data.length; i++) {
      var node = document.createElement("tr")
      var tb = document.createElement("td")
      var textnode = document.createTextNode(data[i].data);
      tb.appendChild(textnode);
      node.appendChild(tb)
      document.getElementById("myList1").appendChild(node);
    }
<table>
  <thead>
    <tr>
      <th data-field="id">Name</th>
      <th data-field="name">Item Name</th>
      <th data-field="price">Item Price</th>
    </tr>
  </thead>

  <tbody id="myList1">

  </tbody>
</table>

I have an array of objects, that I would like to add to my table.

I am failing to understand how can I append this data to a table.

    var data = [{
      id: 12,
      data: 70,
      mama: "sjk"
    }, {
      id: 12,
      data: "mou",
      mama: "sjk"
    }, {
      id: 12,
      data: "mou",
      mama: "sjk"
    }]

    for (var i = 0; i < data.length; i++) {
      var node = document.createElement("tr")
      var tb = document.createElement("td")
      var textnode = document.createTextNode(data[i].data);
      tb.appendChild(textnode);
      node.appendChild(tb)
      document.getElementById("myList1").appendChild(node);
    }
<table>
  <thead>
    <tr>
      <th data-field="id">Name</th>
      <th data-field="name">Item Name</th>
      <th data-field="price">Item Price</th>
    </tr>
  </thead>

  <tbody id="myList1">

  </tbody>
</table>

Share Improve this question edited Feb 13, 2021 at 15:48 peterh 1 asked Dec 25, 2016 at 18:24 PrinceZeePrinceZee 1,5983 gold badges12 silver badges24 bronze badges 2
  • You need to create and append cells in a nested loop. – Teemu Commented Dec 25, 2016 at 18:30
  • or have 3 of your "tb"'s appended to each tr – mplungjan Commented Dec 25, 2016 at 18:32
Add a ment  | 

2 Answers 2

Reset to default 3

Since you've tabular data stored into an array of objects, you could first store the property names into an array. Then loop through the data, and on each round, create cells and their content in a nested loop. Something like this:

var row, cell, text, r, c,
  prop = ['id', 'data', 'mama'],
  table = document.getElementById("myList1");
for (r = 0; r < data.length; r++) {
  row = document.createElement('tr');
  for (c = 0; c < 3; c++) {
    cell = document.createElement('td');
    text = document.createTextNode(data[r][prop[c]]);
    cell.appendChild(text);
    row.appendChild(cell);
  }
  table.appendChild(row);
}

A working demo at jsFiddle.

The key point is the code in the for-loop create HTML table row element and insert into tbody.

  var node = document.createElement("tr") // <tr></tr>
  var tb = document.createElement("td") // <td></td>
  var textnode = document.createTextNode(data[i].data); 
  tb.appendChild(textnode); // insert data into td element <td>data[i].data</td>
  node.appendChild(tb) // insert td element into tr <tr><td>data[i].data</td></tr>
  document.getElementById("myList1").appendChild(node); // insert tr element into tbody, which is myList1

So after the first loop, the myList1 will be

<tbody id="myList1">
    <tr><td>70</td></tr>
</tbody>

after second loop, the myList1 will be

<tbody id="myList1">
    <tr><td>70</td></tr>
    <tr><td>mou</td></tr>
</tbody>

and so on

Here is what you should do

var data = [{
  id: 12,
  data: 70,
  mama: "sjk"
}, {
  id: 12,
  data: "mou",
  mama: "sjk"
}, {
  id: 12,
  data: "mou",
  mama: "sjk"
}]

for (var i = 0; i < data.length; i++) {
  var node = document.createElement("tr")
  for (var key of ['id', 'data', 'mama']) {
    var tb = document.createElement("td")
    tb.innerHTML = data[i][key]
    node.appendChild(tb)
  }
  document.getElementById("myList1").appendChild(node);
}
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>