使用jQuery从数组2维创建表

时间:2013-03-29 12:09:07

标签: javascript jquery html-table

我想从JSON的数组2维创建具有值的表。这是我的代码:

数据

 var data = {"ver[
    {"0":"1","id_pemasang":"1","1":"1","id_jenis_pemasang":"1","2":null,"npwp":null,"3":"Yos Sudarso","nama":"Yos Sudarso","4":"Perumahan Griya Perwita No.10","alamat":"Perumahan Griya Perwita No.10","5":"Yogyakarta","kota":"Yogyakarta","6":"0274-541056","telepon":"0274-541056","7":"08134570378","handphone":"08134570378","8":"0","diskon":"0"},
    {"0":"2","id_pemasang":"2","1":"2","id_jenis_pemasang":"2","2":"34.081.203.1-342.000","npwp":"34.081.203.1-342.000","3":"CV. Prima Agung","nama":"CV. Prima Agung","4":"Jl. Cenderawasih No.72 Warungboto","alamat":"Jl. Cenderawasih No.72 Warungboto","5":"Yogyakarta","kota":"Yogyakarta","6":"0274-878906","telepon":"0274-878906","7":null,"handphone":null,"8":"5","diskon":"5"},
    {"0":"3","id_pemasang":"3","1":"2","id_jenis_pemasang":"2","2":null,"npwp":null,"3":"PT. Tampil Jaya","nama":"PT. Tampil Jaya","4":"Jl. Alamanda No.3","alamat":"Jl. Alamanda No.3","5":"Yogyakarta","kota":"Yogyakarta","6":"0274-552233","telepon":"0274-552233","7":null,"handphone":null,"8":"20","diskon":"20"}
 ]};

HTML

<table class="table table-striped table-bordered table-data-omset">
     <tbody></tbody>
</table>

的jQuery

var tableOmset = $('table.table-data-omset');
var tbodyTableOmset = tableOmset.find('tbody');
var rowTbodyTableOmset = '<tr></tr>';

for(var a=0; a<data.ver.length; a++){
    tbodyTableOmset.append(rowTbodyTableOmset);                       
    for(var b=0; b<5; b++){
        if(data.ver[a][b] == null){
             data.ver[a][b]= '';
        }
        tbodyTableOmset.find('tr').append('<td>'+data.ver[a][b]+'</td>');
     }
 }

但是在我执行或运行之后,结果会成功,但看起来并不好。这是结果图片:

enter image description here

我希望你能解决这个问题。

2 个答案:

答案 0 :(得分:1)

for循环中,使用trTableOmset +=在字符串中创建表格元素。比使用tbodyTableOmset.append(trTableOmset);你已经完成了!

LIVE DEMO

应该如何看待:

var tableOmset = $('table.table-data-omset');
var tbodyTableOmset = tableOmset.find('tbody');
var trTableOmset = '';
for(var a=0; a<data.ver.length; a++){
    trTableOmset += '<tr>'  ;                    
    for(var b=0; b<18; b++){          // 5 or 18 ?
        if(data.ver[a][b] === null){
             data.ver[a][b]= '';
        }
        trTableOmset += '<td>'+ data.ver[a][b] +'</td>';
     }
    trTableOmset += '</tr>' ;
 }
tbodyTableOmset.append(trTableOmset);

固定数据:

var data = {
    "ver": [
        {
            "0": "1",
            "id_pemasang": "1",
            "1": "1",
            "id_jenis_pemasang": "1",
            "2": null,
            "npwp": null,
            "3": "YosSudarso",
            "nama": "YosSudarso",
            "4": "PerumahanGriyaPerwitaNo.10",
            "alamat": "PerumahanGriyaPerwitaNo.10",
            "5": "Yogyakarta",
            "kota": "Yogyakarta",
            "6": "0274-541056",
            "telepon": "0274-541056",
            "7": "08134570378",
            "handphone": "08134570378",
            "8": "0",
            "diskon": "0"
        },
        {
            "0": "2",
            "id_pemasang": "2",
            "1": "2",
            "id_jenis_pemasang": "2",
            "2": "34.081.203.1-342.000",
            "npwp": "34.081.203.1-342.000",
            "3": "CV.PrimaAgung",
            "nama": "CV.PrimaAgung",
            "4": "Jl.CenderawasihNo.72Warungboto",
            "alamat": "Jl.CenderawasihNo.72Warungboto",
            "5": "Yogyakarta",
            "kota": "Yogyakarta",
            "6": "0274-878906",
            "telepon": "0274-878906",
            "7": null,
            "handphone": null,
            "8": "5",
            "diskon": "5"
        },
        {
            "0": "3",
            "id_pemasang": "3",
            "1": "2",
            "id_jenis_pemasang": "2",
            "2": null,
            "npwp": null,
            "3": "PT.TampilJaya",
            "nama": "PT.TampilJaya",
            "4": "Jl.AlamandaNo.3",
            "alamat": "Jl.AlamandaNo.3",
            "5": "Yogyakarta",
            "kota": "Yogyakarta",
            "6": "0274-552233",
            "telepon": "0274-552233",
            "7": null,
            "handphone": null,
            "8": "20",
            "diskon": "20"
        }
    ]
};

答案 1 :(得分:1)

Example

<强> JS

function ToTable(dataArray)
{
    html = '<table>'; 
    var len = dataArray.length;
    for(var i = 0; i < len; i++){
        html += '<tr>'; 
        for(var key in dataArray[i]){
            html += '<td>' + dataArray[i][key] + '</td>'; 
        }
        html += '</tr>';
    }
    html += '</table>'; 
    return html; 
}

data = {
    ver: [
        {"0":"1","id_pemasang":"1","1":"1","id_jenis_pemasang":"1","2":null,"npwp":null,"3":"Yos Sudarso","nama":"Yos Sudarso","4":"Perumahan Griya Perwita No.10","alamat":"Perumahan Griya Perwita No.10","5":"Yogyakarta","kota":"Yogyakarta","6":"0274-541056","telepon":"0274-541056","7":"08134570378","handphone":"08134570378","8":"0","diskon":"0"},
        {"0":"2","id_pemasang":"2","1":"2","id_jenis_pemasang":"2","2":"34.081.203.1-342.000","npwp":"34.081.203.1-342.000","3":"CV. Prima Agung","nama":"CV. Prima Agung","4":"Jl. Cenderawasih No.72 Warungboto","alamat":"Jl. Cenderawasih No.72 Warungboto","5":"Yogyakarta","kota":"Yogyakarta","6":"0274-878906","telepon":"0274-878906","7":null,"handphone":null,"8":"5","diskon":"5"},
        {"0":"3","id_pemasang":"3","1":"2","id_jenis_pemasang":"2","2":null,"npwp":null,"3":"PT. Tampil Jaya","nama":"PT. Tampil Jaya","4":"Jl. Alamanda No.3","alamat":"Jl. Alamanda No.3","5":"Yogyakarta","kota":"Yogyakarta","6":"0274-552233","telepon":"0274-552233","7":null,"handphone":null,"8":"20","diskon":"20"}
    ] 
};

document.getElementById('destination').innerHTML = ToTable(data.ver); 

<强> HTML

<div id='destination'></div>