繁体   English   中英

以父子层次结构显示JSON数据

[英]Display the JSON data in parent-child hierarchical structure

我们想要以父子层次结构显示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/

任何即时帮助将非常可观。 谢谢。

检查这个小提琴

不完全是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 = "Rohit Jindal";
    //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

我已经修改了您的代码。 请看一下小提琴: 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 = "Rohit Jindal";
    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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM