0

このようなJSON文字列を持つIM

 [{"group_id":"1","group_name":"coupler system","subgroups":[{"group_id":"9","group_name":"core","subgroups":0},{"group_id":"10","group_name":"dual","subgroups":0}]},{"group_id":"2","group_name":"powersource\/engine","subgroups":[{"group_id":"11","group_name":"petrol","subgroups":[{"group_id":"13","group_name":"ECO-whisper","subgroups":[{"group_id":"15","group_name":"yes","subgroups":0},{"group_id":"16","group_name":"no","subgroups":0}]}]},{"group_id":"12","group_name":"electric","subgroups":[{"group_id":"14","group_name":"mains","subgroups":[{"group_id":"17","group_name":"220V","subgroups":0},{"group_id":"18","group_name":"110V","subgroups":0}]}]}]},{"group_id":"3","group_name":"tool connections","subgroups":[{"group_id":"19","group_name":"1(STO)","subgroups":0},{"group_id":"20","group_name":"2(MTO)","subgroups":0}]},{"group_id":"4","group_name":"oil content","subgroups":[{"group_id":"21","group_name":"2L?","subgroups":0},{"group_id":"22","group_name":"4L?","subgroups":0},{"group_id":"23","group_name":"6L?","subgroups":0}]},{"group_id":"5","group_name":"hose reels","subgroups":[{"group_id":"24","group_name":"no","subgroups":0},{"group_id":"25","group_name":"yes","subgroups":0}]},{"group_id":"6","group_name":"tool support + carry","subgroups":[{"group_id":"29","group_name":"no","subgroups":0},{"group_id":"30","group_name":"yes","subgroups":[{"group_id":"31","group_name":"wheelset","subgroups":[{"group_id":"32","group_name":"yes","subgroups":0},{"group_id":"33","group_name":"no","subgroups":0}]}]}]},{"group_id":"7","group_name":"weight","subgroups":[{"group_id":"34","group_name":"<=15?","subgroups":0},{"group_id":"35","group_name":"<=25?","subgroups":0},{"group_id":"36","group_name":"<=50?","subgroups":0},{"group_id":"37","group_name":"<=100?","subgroups":0}]},{"group_id":"8","group_name":"dimensions","subgroups":[{"group_id":"38","group_name":"SR10","subgroups":0},{"group_id":"39","group_name":"SR20","subgroups":0},{"group_id":"40","group_name":"SR31\/32","subgroups":0},{"group_id":"41","group_name":"SR40","subgroups":0},{"group_id":"42","group_name":"SR41\/42","subgroups":0}]}] 

Jqueryを使用して、このJSONを以下に示す有効なHTMLテーブル構造にする必要があります。どうすれば実装できますか ここに画像の説明を入力してください

これは私がこれまでに試したことです..imは再帰的なループ部分で打たれました

  <script type="text/javascript">
        $(document).ready(function(){
           var jsondata = jQuery.parseJSON($.trim($('#groupdata').html())); //the above JSON

           var str = '';
           str +='<table>';
           str +='<tr>';




             for(j=0;j<jsondata.length;j++){



                 str +='<td>'+jsondata[j]['group_name']+'</td>';
        // here i want to call the same loop again if it has subgroups


             }
             str +=' </tr>';
             str+='</table>';
             $('#container').html(str);
        });
    </script>
4

3 に答える 3

3

これには 3 つの部分があります。

  • 再帰を行う方法
  • HTML の作成方法
  • jQueryでそれを行う方法

このようなものがある場合は、(a) 単純化する (b) パーツに分解する必要があります。単純なデータを使用して再帰を理解し、HTML を作成します。それが機能している場合は、実際のデータのサブセットを導入し、それを機能させます。

大まかなテンプレート。このjsonを使用します:

var json = '[{ "first": "1", "second": "2", "sub": 0 },{ "first": "3", "second": "4", "sub": [{ "first": "3.1", "second": "3.2", "sub": 0 }] }]';

再帰を行うには、「行ビルダー」を別の関数に分割して、必要なときに自分自身を呼び出す必要があります。

以下のようなもの。独自のデータのためにそれを操作したり、必要なものに html を調整したりする必要があります。最初に行う必要があるのは、それを理解することです。

私はこれが完璧であるとは思いませんし、パフォーマンスが改善されるかもしれませんが、これで正しい方向に向かうはずです...幸せな解析...

$(document).ready(function () {
    var data = $.parseJSON(json);
    var html = "<table>";
    var rows = recurse(data);
    html += rows + "</table>";
    document.write(html);
});

function recurse(data) {
    var row = "";
    for (var e in data) { //each object at this level
        row += "<tr>";
        row += "<td>" + data[e].first + "</td>";
        row += "<td>" + data[e].second + "</td>";
        row += "<td>";
        if (data[e].sub.length == 1) {
            var sub = "<table>";
            sub += recurse(data[e].sub)
            sub += "</table>";
            row += sub;
        }
        row += "</td></tr>";
    }
    return row;
}
于 2012-08-03T04:57:01.480 に答える
0

再帰には関数が必要です。したがって、ビルドします。

function generateTableMarkup(data) {
    for (var i=0; i<data.length; i++) {
        // do something
        if (data[i].subgroups) {
            generateTableMarkup(data[i].subgroups);
        }
    }
}
generateTableMarkup(JSON.parse(...));
于 2012-08-03T04:31:20.773 に答える