4

I want to create table with value from array 2 dimension from JSON. This is the code I made:

Data

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

but after I execute or run, the result has success, but it appear not good. this is result image:

enter image description here

I hope you can help me from this problem.

5
  • and the questionmark is... ? btw your data is errata jsonlint.com Commented Mar 29, 2013 at 12:11
  • your json is invalid.. bytheway Commented Mar 29, 2013 at 12:12
  • @bipen: no, my json is valid, check again. Commented Mar 29, 2013 at 12:21
  • hahaha.. sorry to say this.. but again its invalid ...u forgot : ..i could have edited this... but not sure if that is a typo or if that is what you are getting.. if that is what your are gettin then the problem is in JSON and not in jquery..:) Commented Mar 29, 2013 at 12:22
  • stackoverflow.com/questions/1051061/… Commented Mar 29, 2013 at 12:23

2 Answers 2

1

In your for loops create your table elements in string using trTableOmset +=. Than using tbodyTableOmset.append(trTableOmset); you're done!

LIVE DEMO

How it should look:

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

Fixed DATA:

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
  • yeah, your answer that's all right. I forget about += can help this. thank you very much guys :) Commented Mar 29, 2013 at 12:38
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>
2
  • for(i=N; i<V; i++){ is faster, and ver should be "ver" following the precise valid JSON structure recipes. Commented Mar 29, 2013 at 12:24
  • @roXon - True, but then I need to know the number of columns ahead of time. Probably better if his JSON were a 2 dimensional array. Smaller payload and easier to iterate over. Commented Mar 29, 2013 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.