【问题标题】:Display the JSON data in parent-child hierarchical structure以父子层次结构显示 JSON 数据
【发布时间】:2016-07-01 19:23:11
【问题描述】:

我们希望以父子层次结构显示 JSON 数据

var JsonArr = [{
                   "comment":"Comment 1", 
                   "commentID":1, 
                   "parentID":0,
                   "children":[{
                              "comment":"Comment 1-2", 
                              "commentID":2, 
                              "parentID":1, 
                              "children":[{
                                         "comment":"Comment 1-2-2",
                                         "commentID":1,
                                         "parentID":2 
                                         }]
                            }]
                },
                {
                "comment":"Comment 2", 
                "commentID":4, 
                "parentID":0
               }]

电流输出:

预期输出:

工作 JSFiddle : https://jsfiddle.net/6dcdbks4/

任何即时帮助都将受到高度评价。谢谢。

【问题讨论】:

    标签: javascript jquery json


    【解决方案1】:

    检查这个fiddle

    不完全是 tree 结构,但我们可以使用 css 模拟它并传递 level 信息

    function showComments(comments,level){//Extra parameter for level information
     for(var i = 0; i < comments.length; i++) {
         commentsContainer = loadComment(comments[i], commentsContainer,level)//render comment along with level information
        if (comments[i]['children'] && comments[i]['children'].length) {
          showComments(comments[i]['children'],level+1)//next level for children
        }
      }
    }
    
    function loadComment(commentObj, commentsContainer,level){//level of node
        var profileFullName = "Full Name";
        //add some padding multiplied with level for each comment element
        var commentHTML = '<div class="commentbox" style="padding-left:'+(level*100)+'px;"><div class="commentphoto"><a href="#123"><img src="https://graph.facebook.com/100000816365798/picture?type=square"></a></div><div class="commentcontent"><a href="#123"><strong>' + profileFullName + '</strong></a> &nbsp;<small>Just now</small><br>' + commentObj.comment + '<br><a name="replyComment" commentid="' + commentObj.commentID + '">Reply</a><span id="votescore-' + commentObj.commentID + '" class="votescore"></span></div></div>';
        commentsContainer.append(commentHTML);
        return commentsContainer;
    }
    
    showComments(JsonArr,0);//call showComment with initial level 0
    

    【讨论】:

    • 我更新了你的小提琴..我改变了 json 并且它不起作用。 jsfiddle.net/jgupggnz/2
    • 你能再检查一次数据吗?Comment 1-2你保留了两个“子”数组
    • 是的..有两个同父异母的孩子是可能的。
    • 但这不对。如果您有多个孩子,为什么不将它们推入一个孩子数组中?
    • 评论Comment 1-2你有两个子cmets,那么你应该有children数组,其中有Comment 1-2-1Comment 1-2-2。但是你在小提琴中的数据有两个children属性。
    【解决方案2】:

    我稍微修改了您的代码。请看小提琴:https://jsfiddle.net/swaprks/6dcdbks4/2/

    JAVASCRIPT:

    $(document).ready(function() {
     var StmId = $('[name = "StatementId"]').val();
     var JsonArr = [{
                       "comment":"Comment 1", 
                       "commentID":1, 
                       "parentID":0,
                       "children":[{
                                  "comment":"Comment 1-2", 
                                  "commentID":2, 
                                  "parentID":1, 
                                  "children":[{
                                             "comment":"Comment 1-2-2",
                                             "commentID":1,
                                             "parentID":2 
                                             }]
                                }]
                    },
                    {
                    "comment":"Comment 2", 
                    "commentID":4, 
                    "parentID":0
                   }]
    
    var commentsContainer = $("<div></div>");
    
    showComments(JsonArr);
    
    function loadComment(commentObj, commentsContainer, isChild){
       // console.log(commentObj);
        var profileFullName = "Full Name";
        var marginLeft = '';
        if ( commentObj.parentID > 0 ) {
             marginLeft = 'style="margin-left: '+commentObj.parentID*60+'px;"'
        }
        
        var commentHTML = '<div '+marginLeft+' class="commentbox"><div class="commentphoto"><a href="#123"><img src="https://graph.facebook.com/100000816365798/picture?type=square"></a></div><div class="commentcontent"><a href="#123"><strong>' + profileFullName + '</strong></a> &nbsp;<small>Just now</small><br>' + commentObj.comment + '<br><a name="replyComment" commentid="' + commentObj.commentID + '">Reply</a><span id="votescore-' + commentObj.commentID + '" class="votescore"></span></div></div>';
        commentsContainer.append(commentHTML);
           // console.log(commentsContainer.closest('.commentbox'));
        return commentsContainer;
    }
    
    function showComments(comments, isChild){
     for(var i = 0; i < comments.length; i++) {
        // console.log(comments[i]['comment']);
           commentsContainer = loadComment(comments[i], commentsContainer, isChild)
        if (comments[i]['children'] && comments[i]['children'].length) {
          showComments(comments[i]['children'], true)
        }
      }
    }
    
    $('.commentbox').append(commentsContainer);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多