JSON 到 ajax 中的 HTML 表

新手上路,请多包涵

这是我的 JSON

 {
   "version": "5.2",
   "user_type": "online",
   "user":
   [
       {
            "name": "John",
            "id": 50
       },
       {
            "name": "Mark",
            "id": 57
        }
    ]
}

将上述 JSON 转换为 HTML 的 javascript

 <script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        url: "http://PATH/user.json",
        dataType: 'json',
        type: 'get',
        cache:false,
        success: function(data){
            /*console.log(data);*/
            var event_data = '';
            $.each(data, function(index, value){
                /*console.log(value);*/
                event_data += '<tr>';
                event_data += '<td>'+value.name+'</td>';
                event_data += '<td>'+value.id+'</td>';
                event_data += '<tr>';
            });
            $("#list_table_json").append(event_data);
        },
        error: function(d){
            /*console.log("error");*/
            alert("404. Please wait until the File is Loaded.");
        }
    });
});
</script>

表的 HTML 代码:

 <table class="table table-responsive table-hover table-bordered" id="list_table_json">
    <thead>
        <tr>
            <th>Name</th>
            <th>ID</th>
        </tr>
    </thead>
</table>

在控制台中没有收到任何错误

我在表中得到的输出显示未定义。如何将数据推送到json?

原文由 deepak murthy 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 252
2 个回答

你的循环应该像 $.each(data.user, function(index, value){}); 并关闭 </tr> 最后

<script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        url: "http://PATH/user.json",
        dataType: 'json',
        type: 'get',
        cache:false,
        success: function(data){
            /*console.log(data);*/
            var event_data = '';
            $.each(data.user, function(index, value){
                /*console.log(value);*/
                event_data += '<tr>';
                event_data += '<td>'+value.name+'</td>';
                event_data += '<td>'+value.id+'</td>';
                event_data += '</tr>';
            });
            $("#list_table_json").append(event_data);
        },
        error: function(d){
            /*console.log("error");*/
            alert("404. Please wait until the File is Loaded.");
        }
    });
});
</script>

原文由 akbansa 发布,翻译遵循 CC BY-SA 3.0 许可协议

这是我可以推荐 Vue 的情况。寻找下一个(工作)示例:

 var table = new Vue({
  el: '#dynamic',
  data: {
    users: []
  }
})

// And on click just assign loaded JSON to component model
// and view (table) will be re-rendered automatically
$('button').click(_ =>
  $.getJSON('https://api.mockaroo.com/api/03f2c610?count=20&key=cdbbbcd0')
   .done(data => table.$data.users = data)
)
 #dynamic {
  border-collapse: collapse;
}
#dynamic th, #dynamic td {
  border: 1px solid black;
}
 <script src="https://unpkg.com/jquery@3.2.1/dist/jquery.min.js"></script>
<script src="https://unpkg.com/vue@2.4.2/dist/vue.min.js"></script>

<button>Load some fake data from mockaroo.com</button>

<!-- Prepare table in clean, readable way -->
<table id="dynamic">
  <thead>
    <tr>
      <th>ID</th>
      <th>Nick</th>
      <th>First name</th>
      <th>Last name</th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="row in users">
      <td v-for="column in row">{{ column }}</td>
    </tr>
  </tbody>
</table>

现在,如果您希望此表可排序,只需几行代码即可:

 var table = new Vue({
  el: '#dynamic',
  data: {
    users: []
  },
  methods: {
    sort (e) { // This is the method definition
      var key = e.target.dataset.key // read data-key value from clicked th
      this.users = _.sortBy(this.users, key) // sortBy method is from underscore
    }
  }
})

// And on click just assign loaded JSON to component model
// and view (table) will be re-rendered automatically
$('button').click(_ =>
  $.getJSON('https://api.mockaroo.com/api/03f2c610?count=20&key=cdbbbcd0')
   .done(data => table.$data.users = data)
)
 #dynamic {
  border-collapse: collapse;
}
#dynamic th, #dynamic td {
  border: 1px solid black;
}
#dynamic th {
  cursor: pointer;
}
 <script src="https://unpkg.com/jquery@3.2.1/dist/jquery.min.js"></script>
<script src="https://unpkg.com/vue@2.4.2/dist/vue.min.js"></script>
<!-- Underscore have some great methods for lists, array and objects -->
<script src="https://unpkg.com/underscore@1.8.3/underscore-min.js"></script>

<button>Load some fake data from mockaroo.com</button>

<!-- Prepare table in clean, readable way -->
<table id="dynamic">
  <thead>
    <tr @click="sort"><!-- Run sort method on click -->
      <th data-key="id">ID</th>
      <th data-key="nick">Nick</th>
      <th data-key="first">First name</th>
      <th data-key="last">Last name</th>
      <th data-key="email">Email</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="row in users">
      <td v-for="column in row">{{ column }}</td>
    </tr>
  </tbody>
</table>

原文由 user6748331 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题