1

我需要一个树结构的帮助,这里有代码,我从网上得到的,我需要用展开和折叠功能来显示它......

var treeNodes = [
                             {parent:-1,id:0,name:'root'},
                             {parent:1,id:3,name:'net'},
                             {parent:1,id:4,name:'flash '},
                             {parent:3,id:7,name:'South korea'},
                             {parent:6,id:8,name:'West korea'},
                             {parent:0,id:1,name:'Microsoft'},
                             {parent:1,id:5,name:'Google'},                          
                             {parent:0,id:2,name:'JPmorgon Intl'},
                             {parent:2,id:6,name:'SW'}
                         ];
            $("#container").append( buildNestedList(treeNodes, 0) );


            function buildNestedList(treeNodes, rootId) {
                  var nodesByParent = {};

                  $.each(treeNodes, function(i, node) {
                    if (!(node.parent in nodesByParent)) nodesByParent[node.parent] = [];
                    nodesByParent[node.parent].push(node);
                  });

                  function buildTree(children) {
                    var $container = $("<ul>");
                    if (!children) return;
                    $.each(children, function(i, child) {
                      $("<li>", {text: child.name})
                      .appendTo($container)
                      .append( buildTree(nodesByParent[child.id]) );
                    });
                    return $container;
                  }
                  return buildTree(nodesByParent[rootId]);
                }

任何人请指导我。我需要设置 + 和 _ 图像来展开和折叠树结构。

提前致谢

4

0 回答 0