繁体   English   中英

试图用 json 填充 html 表

[英]Trying to fill a html table with json

我为这个愚蠢的问题道歉,我是一个初学者,对 Javascript、jQuery、Ajax 的组合感到很困惑……

我有一个 json 文件,位于 json/bdd.json 中:

{
"donnees" : [

        {
            "id": 1,
            "date": "01/01/2020",
            "name": "Pâques",
            "tag": "Fête",
            "long": "5min",
            "icon":"<i class='fab fa-itunes-note'>"
        },
        {
            "id": 2,
            "date": "01/10/2020",
            "name": "Anniversaire",
            "tag": "Fête",
            "long": "21min",
            "icon":"<i class='fab fa-itunes-note'>"
        },
        {
            "id": 3,
            "date": "01/03/2020",
            "name": "Conférence",
            "tag": "Travail",
            "long": "56min",
            "icon":"<i class='far fa-file-alt'></i>"
        }
]}

我想像这样填写 html 表:

<table class="table">
  <thead class="black white-text">
    <tr>
      <th scope="col">#</th>
      <th scope="col">Date</th>
      <th scope="col">Nom</th>
      <th scope="col">Tag</th>
      <th scope="col">durée</th>
      <th scope="col"><i class="fab fa-itunes-note"></i> / <i class="far fa-file-alt"></i></th>
    </tr>
  </thead>
  <tbody id="table-body">
  </tbody>
</table>

编辑:问题已解决,它适用于当前代码:

$( document ).ready(function() {
 $.getJSON('json/bdd.json', function(i, donnees) { 

  $.each(i.donnees, function (index, element) {

  let row = (`<tr>
  <td scope="col">${element.id}</td>
  <td scope="col">${element.date}</td>
  <td scope="col">${element.name}</td>
  <td scope="col">${element.tag}</td>
  <td scope="col">${element.long}</td>
  <td scope="col">${element.icon}</td>
</tr>`);


$('#table-body').append(row);
 });

 });

 });

谢谢你的帮助:)

您可以在 getJSON 回调中使用$.each()填充您的表。

$.getJSON('json/bdd.json', function(i, donnees) {

  // Add this line
  donnees = JSON.parse(donnees);

  $.each(donnees, function(index, element) {
    let row = jQuery(`<tr>
      <th scope="col">${element.id}</th>
      <th scope="col">${element.date}</th>
      <th scope="col">${element.name}</th>
      <th scope="col">${element.tag}</th>
      <th scope="col">${element.length}</th>
      <th scope="col">${element.icon}</th>
    </tr>`);

    $('#table-body').append(row);
  });

});

是的,您确实需要遍历 object 阵列。 我个人的偏好是 forEach 循环

例如: donnees.forEach((row) =>... // Append a row to the table )

更新:你试过这个吗?

  $( document ).ready(function() {
     $.getJSON('json/bdd.json', function(i, donnees) { 

        $.each(i, function (index, element) {
            var row = '<tr>'+
                            '<td scope="col">'+element.id+'</td>'+
                            '<td scope="col">'+element.date+'</td>'+
                            '<td scope="col">'+element.name+'</td>'+
                            '<td scope="col">'+element.tag+'</td>'+
                            '<td scope="col">'+element.long+'</td>'+
                            '<td scope="col">'+element.icon+'</td>'+
                        '</tr>';

            $('#table-body').prepend(row);
        });

     });
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM