0

<table>JavaScript関数を介して作成しようとしています。

次のような JSON 要素を取得しています。

header
    ["Nom", "Région", "Activité", 10 more...]
0   "Nom"   
1   "Région"
2   "Activité"
// other fields 

body
    Object { entity0=[13], entity1=[13], entity2=[13], more...}

entity0
    ["Org2", "Org2", "Org2", 10 more...]

0    "Org2"
1    "Org2" 
2    "Org2"
//Other fields
entity1
    ["gfhu", "rtyud", "dgud", 10 more...]
//Other entities

そして、私はそれをそのようにデコードしようとしています(JSONを解析してその関数に渡します):

function createTableEntity(tab, id){
    table = '<table cellpadding="0" cellspacing="0" border="0" class="display" id="'+id+'">';
    table = table + '<thead><tr>';
    $(tab.header).children().each(function(){
        table = table + '<td>' + this + '</td>';
    });
    table = table + '</tr></thead>';

    table = table + '<tbody>';

    $(tab.body).children().each(function(){
        table = table + '<tr>';

       $(this).children().each(function(){
           table = table + '<td>' + $(this) + '</td>';
       });

       table = table + '</tr>';
    });

    table = table + '</tbody>';

    table = table + '</table>';

    //alert(table);

    return table;
}

私が持っている結果から、子供はいません($(tab.header).children().each(function(){});)。

それはどこから来たのですか?JSON から解析された要素をループするにはどうすればよいですか?

4

1 に答える 1

1

JSON 解析の結果は Javascript オブジェクトであるため、jquery でループ処理する必要はありません。

tab.header に配列がある場合は、単純にループすることができます

  $.each(tab.header, function() {

または、より古典的に、jquery を使用せずに

 for (var i=0; i<tab.header.length; i++) {
于 2012-06-22T11:05:23.330 に答える