显示节点树

时间:2012-10-29 22:38:37

标签: jquery data-visualization

  

可能重复:
  Javascript library for node-based objects and object relationships?

是否有任何Jquery插件或任何解决方案,用于显示节点关系表(一个节点有一个父节点和多个子节点)给出这个数据表?

1 个答案:

答案 0 :(得分:0)

您希望以何种方式展示它们?

以下是一些插件的列表:http://www.webdesignbooth.com/15-great-jquery-plugins-for-better-table-manipulation/

我可以告诉你,你可以根据自己的需要进行修改。它正在做的是通过对象图递归。请注意,可以通过编辑function newDiv代码来更改要进行此操作的样式。

(function () {
     var jsonData = dataSentFromServer();
     var detailsElement = document.getElementById("Details");
     function newDiv(txt) {
         var createDiv = document.createElement("div");
         if (txt != undefined)
             createDiv.innerHTML = txt;
         return createDiv;
     }

     var depth = 1;
     (function ComposeGraph(obj, el) {
         var ElementArray = [];
         var ChildArray = [];
         $.each(obj, function (name, value) {
             if (!$.isArray(value)) {

                 var appender = newDiv(name + " __:__ " + value);
                 appender.setAttribute("style", "margin-left:" + 10 * depth + "px;");
                 if ($.isPlainObject(value)) {
                     appender.innerHTML = "<b>" + appender.innerHTML + "</b>";
                     depth++;
                     ComposeGraph(value, appender);
                     depth--;
                     ElementArray.push(el);
                     ChildArray.push(appender);
                 } else {
                     el.appendChild(appender);
                 }
             } else {
                 var appender = newDiv(name + " __:__ " + value);
                 appender.setAttribute("style", "margin-left:" + 10 * depth + "px;");
                 appender.innerHTML = "<b>" + appender.innerHTML + "</b>";
                 for (var i = 0, len = value.length; i < len; i++) {
                     depth++;
                     ComposeGraph(value[i], appender);
                     depth--;
                 }
                 ElementArray.push(el);
                 ChildArray.push(appender);
             }
         });
         for (var i = 0, len = ElementArray.length; i < len; i++) {
             ElementArray[i].appendChild(ChildArray[i]);
         }
     })(jsonData, detailsElement);
 })();