从JSON数据填充大表行 [英] Filling a Big Table Rows from JSON Data

查看:85
本文介绍了从JSON数据填充大表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JSON数据填充表格

I am trying to fill up a table using JSON data

这就是我的想法

$.ajax({
  dataType: "json",
  url: "music.json"
})
.done(function(gamesjson){
    DATA = gamesjson;
    buildTable(DATA ); // this one is calling the above code
  })
.fail(function(){
    console.log("music.json error");
  })
;

function buildTable(DATA){
var gl = $("#gl");
$.each(DATA.music, function(index, value) {
// code to populate table
??

}

$("#gl").append(gl);
}

我在buildTable()中写什么? 如果我的json包含1000行,那么构建表的最佳方法是什么?

What do I write in buildTable()? Also if my json contains 1000 rows, what's the best way to build table?

请提供示例和说明.谢谢

Please give an example and explanation. Thanks

http://jsfiddle.net/9u4zR/1/

推荐答案

类似以下内容:

function buildTable(DATA){
    var table = "<table>";
    $.each(DATA.music, function(index, value) {
        table += "<tr><td>" + value.col1 + "</td><td>" + value.col2 + "</td></tr>";
    }
    table += "</table>";
    $("#gl").append(table);    
}

当然,您需要用实际属性的名称替换col1col2,并根据需要添加更多列.您可能还需要指定类或样式.但这向您显示了一般结构,您可以根据应用程序的需要对其进行优化.

Of course, you need to replace col1 and col2 with the names of your actual properties, and add more columns as needed. You'll also probably want to specify classes or styles. But this shows you the general structure, you can refine it as needed for your application.

这篇关于从JSON数据填充大表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆