4

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>');
     }
 }

しかし、実行または実行した後、結果は成功しましたが、良くないようです。これは結果画像です:

ここに画像の説明を入力

この問題から私を助けてくれることを願っています。

4

2 に答える 2

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"
        }
    ]
};
于 2013-03-29T12:21:12.810 に答える
1

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>
于 2013-03-29T12:23:20.527 に答える