lvk0804 2009-08-20 13:23
浏览 161
已采纳

资源树

function SkyObject(){
var id = 0;
var className = "SkyObject";

this.getId = function(){
return id;
}
this.setId = function(_id){
id = _id;
}
this.setClassName = function(name){
className = name;
}
this.getClassName = function(){
return className
}

addToContainer(this);

this.getElement = function(){
return window.document.getElementById("obj_"+ this.getId());
}
}

var hxdObjPointer = 0;
var hxdContainer = [];

function addToContainer(obj){
obj.setId(hxdObjPointer);
hxdContainer[hxdObjPointer] = obj;
hxdObjPointer ++;
}

function getFromContainer(id){
return hxdContainer[id];
}

function stateClick(id){
var node = getFromContainer(id);
node.changeState();
}

function nodeClick(id){
var node = getFromContainer(id);
node.onSelected();
var root = node.getRoot();
var preNodeId = root.getPreNodeId();
var preNode = getFromContainer(preNodeId);
if(preNode!=null)
preNode.onUnSelected();
}

function Node(data,parentNode){
SkyObject.call(this);
var pNode = parentNode;
var value = data;
var state = "closed";
var children = [];
this.setClassName("Node");
var childrenInited = false;

this.getRoot = function(){
var current = this;
var parent = null;
while(true){
try{
parent = current.getParentNode();
}catch(e){
}

if(parent==null || parent=="undefined"){
return current;
}else{
current = parent;
parent = null;
}
}
}

this.getParentNode = function() {
return pNode;
}

this.getValue = function(){
return value;
}

this.onSelected = function(){
var element = window.document.getElementById("namelink_"+this.getId());
element.style.color="blue";
var root = this.getRoot();
root.setCurrentNodeId(this.getId());
root.onNodeChange();
}

this.onUnSelected = function(){
var element = window.document.getElementById("namelink_"+this.getId());
if(element!=null)
element.style.color="black";
if(this.getId()==root.getCurrentNodeId()){
var root = this.getRoot();
root.setCurrentNodeId(-1);
}
}

this.removeChild = function(node){
if(node==null || node=="undefined")
return;
var newChildren = [];
var index = 0;
for(var i=0;i<children.length;i++){
var child = children[i];
if(node.getId()!=child.getId()){
newChildren[index] = child;
index++;
}
}

delete children;
children = newChildren;

var element = window.document.getElementById("children_"+this.getId());
if(element!=null){
if(children.length>0)
element.removeChild(node.getElement());
else
this.getElement().removeChild(element);
}
this.repaint();
}

this.addChild = function(node){
children[children.length] = node;
var element = window.document.getElementById("children_"+this.getId());

if(element==null){
if(value.children!=null && value.children.length>0){
this.paintChildren();
element = window.document.getElementById("children_"+this.getId());
}
element = window.document.createElement("div");
element.id="children_"+this.getId();
this.getElement().appendChild(element);
}
state="opened";
element.style.display = "block";
node.paint(element);
this.repaint();
}

this.changeState = function(){
var stateLink = window.document.getElementById("statelink_"+this.getId());
if(state=="opened"){
stateLink.innerHTML=" + ";
state="closed";
}else{
state="opened";
stateLink.innerHTML=" - ";
}

var childrenElement = window.document.getElementById("children_"+this.getId());
if(childrenElement==null){
if(state=="opened") this.paintChildren();
}else{
if(state=="opened"){
childrenElement.style.display = "block";
}else{
childrenElement.style.display = "none";
}
}
}

this.paintChildren = function(){
childrenInited = true;
var nodeElement = this.getElement();
var childrenElement = null;
if(value.children!=null && value.children.length>0){
childrenElement = window.document.createElement("div");
childrenElement.id="children_"+this.getId();
for(var i=0;i < value.children.length;i++){
var childNode = new Node(value.children[i],this);
children[i]=childNode;
childNode.paint(childrenElement);
}
childrenElement.style.display = "block";
nodeElement.appendChild(childrenElement);
}
}

this.repaint = function(){
var statelink = document.getElementById("statelink_"+this.getId());
var namelink = document.getElementById("namelink_"+this.getId());

if(children!=null && children.length>0){
if(state=="opened"){
statelink.innerHTML=" - ";
}else{
statelink.innerHTML=" + ";
}
statelink.href='javascript:stateClick(' + this.getId() +')';
}else{
statelink.innerHTML=" . ";
}
namelink.innerHTML = value.name;
}

this.paint = function(parent){
var nodeElement = window.document.createElement("div");
nodeElement.style.position = "relative";
nodeElement.id = "obj_"+ this.getId();

var statelink = window.document.createElement("a");
statelink.id = "statelink_"+this.getId();
if(value.children!=null && value.children.length>0){
if(state=="opened"){
statelink.innerHTML=" - ";
}else{
statelink.innerHTML=" + ";
}
statelink.href='javascript:stateClick(' + this.getId() +')';
}else{
statelink.innerHTML=" . ";
}

nodeElement.appendChild(statelink);

var namelink = window.document.createElement("a");
namelink.id = "namelink_" + this.getId();
namelink.href='javascript:nodeClick(' + this.getId() + ')';
namelink.innerHTML = value.name;
nodeElement.appendChild(namelink);

if(state=="opened"){
paintChildren();
}
if(parent.type=="nodesPane")
nodeElement.style.left = 2;
else
nodeElement.style.left = 20;
parent.appendChild(nodeElement);
}

}

function Tree(){

SkyObject.call(this);
var children = [];
var title = "title";
var element = null;
var parent = null;
var value = null;
var currentNodeId = -1;
var preNodeId = -1;

this.setCurrentNodeId = function(id){
preNodeId = currentNodeId;
currentNodeId = id;
}

this.getPreNodeId = function(){
return preNodeId;
}

this.getCurrentNodeId = function(){
return currentNodeId;
}

this.getCurrentNode = function(){
if(currentNodeId<0){
return null;
}else
return getFromContainer(currentNodeId);
}

this.onNodeChange = function(){};

this.bindData = function(data){
value = data;
}

this.addChild = function(data){
var node = new Node(data,this);
children[children.length] = node;
var treeElement = this.getElement();
node.paint(treeElement);
}

this.addChildToSelectedNode = function(data){
var selectednode = this.getCurrentNode();
var node = new Node(data,selectednode);
selectednode.addChild(node);
}

this.removeChild = function(node){
var parent = node.getParentNode();
if(parent.getId()==this.getId()){
var element = this.getElement();
element.removeChild(node.getElement());
var newChildren = [];
var index = 0;
for(var i=0;i<children.length;i++){
var child = children[i];
if(node.getId()!=child.getId()){
newChildren[index] = child;
index++;
}
}
delete children;
children = newChildren;

}else{
parent.removeChild(node);
}

}

this.getElement = function(){
var nodesPane = window.document.getElementById("obj_"+ this.getId())
if(nodesPane==null){
nodesPane = window.document.createElement("div");
nodesPane.id = "obj_"+this.getId();
nodesPane.type = "nodesPane";
if(value!=null && value.length>0){
for(var i=0;i<value.length;i++){
var node = new Node(value[i],this);
children[i] = node;
node.paint(nodesPane);
}
}
}
return nodesPane;
}

this.paint = function(parent){
var nodesPane = window.document.createElement("div");
nodesPane.id = "obj_"+this.getId();
if(value!=null && value.length>0){
for(var i=0;i<value.length;i++){
var node = new Node(value[i],this);
children[i] = node;
node.paint(nodesPane);
}
}
parent.appendChild(nodesPane);
}
}

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


tree.html
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript" src="SigmaTree.js"/></script> 
 <script language="javascript"> 
 var tree1 = null; 

var tree2 = null;
window.onload = function(){
var roots = {tree:[
{id:"1",name:"name1",children:
[

{id:"10",name:"child0",children:[]},
{id:"11",name:"child1",children:[]},
{id:"12",name:"child2",children:[]},
{id:"13",name:"child3",children:[]},
{id:"14",name:"child4",children:[]}
]
},

{id:"2",name:"name2",children:[]},
{id:"3",name:"name3",children:[]} ,
{id:"4",name:"name4",children:[]},
]};
var parent1 = document.getElementById("tree1");
tree1 = new Tree();
tree1.bindData(roots.tree);
tree1.paint(parent1);
tree1.onNodeChange = function(){

}
var parent2 = document.getElementById("tree2");

tree2 = new Tree();

tree2.bindData(roots.tree);
tree2.paint(parent2);

tree2.onNodeChange = function(){

}
}

function addNew(){
var nodedata = {id:"123",name:"newNode123"};
tree1.addChild(nodedata);
}

function addNewToSelected(){
var nodedata = {id:"123",name:"newNode123"};
tree1.addChildToSelectedNode(nodedata);
}

function removeSelectedNode(){
var node = tree1.getCurrentNode();
tree1.removeChild(node);
}


资源树显示不出来~总是说缺少对象?谁能帮忙解决这个问题~
谢啦~
[b]问题补充:[/b]
没有啊;我试了很多次了;总是出现‘value.children’为空或者对象不存在~这是怎么回事啊~我查了代码应该没错误啊~

  • 写回答

3条回答 默认 最新

  • wanghaolovezlq 2009-08-20 14:22
    关注

    我搞了一下,没问题哦

    可能是你没搞好吧

    我给你发一下:

    SigmaTree.js
    [code="java"]
    function SkyObject() {
    var id = 0;
    var className = "SkyObject";

    this.getId = function() {
        return id;
    }
    this.setId = function(_id) {
        id = _id;
    }
    this.setClassName = function(name) {
        className = name;
    }
    this.getClassName = function() {
        return className
    }
    
    addToContainer(this);
    
    this.getElement = function() {
        return window.document.getElementById("obj_" + this.getId());
    }
    

    }

    var hxdObjPointer = 0;
    var hxdContainer = [];

    function addToContainer(obj) {
    obj.setId(hxdObjPointer);
    hxdContainer[hxdObjPointer] = obj;
    hxdObjPointer++;
    }

    function getFromContainer(id) {
    return hxdContainer[id];
    }

    function stateClick(id) {
    var node = getFromContainer(id);
    node.changeState();
    }

    function nodeClick(id) {
    var node = getFromContainer(id);
    node.onSelected();
    var root = node.getRoot();
    var preNodeId = root.getPreNodeId();
    var preNode = getFromContainer(preNodeId);
    if (preNode != null)
    preNode.onUnSelected();
    }

    function Node(data, parentNode) {
    SkyObject.call(this);
    var pNode = parentNode;
    var value = data;
    var state = "closed";
    var children = [];
    this.setClassName("Node");
    var childrenInited = false;

    this.getRoot = function() {
        var current = this;
        var parent = null;
        while (true) {
            try {
                parent = current.getParentNode();
            } catch (e) {
            }
    
            if (parent == null || parent == "undefined") {
                return current;
            } else {
                current = parent;
                parent = null;
            }
        }
    }
    
    this.getParentNode = function() {
        return pNode;
    }
    
    this.getValue = function() {
        return value;
    }
    
    this.onSelected = function() {
        var element = window.document
                .getElementById("namelink_" + this.getId());
        element.style.color = "blue";
        var root = this.getRoot();
        root.setCurrentNodeId(this.getId());
        root.onNodeChange();
    }
    
    this.onUnSelected = function() {
        var element = window.document
                .getElementById("namelink_" + this.getId());
        if (element != null)
            element.style.color = "black";
        if (this.getId() == root.getCurrentNodeId()) {
            var root = this.getRoot();
            root.setCurrentNodeId(-1);
        }
    }
    
    this.removeChild = function(node) {
        if (node == null || node == "undefined")
            return;
        var newChildren = [];
        var index = 0;
        for ( var i = 0; i < children.length; i++) {
            var child = children[i];
            if (node.getId() != child.getId()) {
                newChildren[index] = child;
                index++;
            }
        }
    
        delete children;
        children = newChildren;
    
        var element = window.document
                .getElementById("children_" + this.getId());
        if (element != null) {
            if (children.length > 0)
                element.removeChild(node.getElement());
            else
                this.getElement().removeChild(element);
        }
        this.repaint();
    }
    
    this.addChild = function(node) {
        children[children.length] = node;
        var element = window.document
                .getElementById("children_" + this.getId());
    
        if (element == null) {
            if (value.children != null && value.children.length > 0) {
                this.paintChildren();
                element = window.document.getElementById("children_"
                        + this.getId());
            }
            element = window.document.createElement("div");
            element.id = "children_" + this.getId();
            this.getElement().appendChild(element);
        }
        state = "opened";
        element.style.display = "block";
        node.paint(element);
        this.repaint();
    }
    
    this.changeState = function() {
        var stateLink = window.document.getElementById("statelink_"
                + this.getId());
        if (state == "opened") {
            stateLink.innerHTML = " + ";
            state = "closed";
        } else {
            state = "opened";
            stateLink.innerHTML = " - ";
        }
    
        var childrenElement = window.document.getElementById("children_"
                + this.getId());
        if (childrenElement == null) {
            if (state == "opened")
                this.paintChildren();
        } else {
            if (state == "opened") {
                childrenElement.style.display = "block";
            } else {
                childrenElement.style.display = "none";
            }
        }
    }
    
    this.paintChildren = function() {
        childrenInited = true;
        var nodeElement = this.getElement();
        var childrenElement = null;
        if (value.children != null && value.children.length > 0) {
            childrenElement = window.document.createElement("div");
            childrenElement.id = "children_" + this.getId();
            for ( var i = 0; i < value.children.length; i++) {
                var childNode = new Node(value.children[i], this);
                children[i] = childNode;
                childNode.paint(childrenElement);
            }
            childrenElement.style.display = "block";
            nodeElement.appendChild(childrenElement);
        }
    }
    
    this.repaint = function() {
        var statelink = document.getElementById("statelink_" + this.getId());
        var namelink = document.getElementById("namelink_" + this.getId());
    
        if (children != null && children.length > 0) {
            if (state == "opened") {
                statelink.innerHTML = " - ";
            } else {
                statelink.innerHTML = " + ";
            }
            statelink.href = 'javascript:stateClick(' + this.getId() + ')';
        } else {
            statelink.innerHTML = " . ";
        }
        namelink.innerHTML = value.name;
    }
    
    this.paint = function(parent) {
        var nodeElement = window.document.createElement("div");
        nodeElement.style.position = "relative";
        nodeElement.id = "obj_" + this.getId();
    
        var statelink = window.document.createElement("a");
        statelink.id = "statelink_" + this.getId();
        if (value.children != null && value.children.length > 0) {
            if (state == "opened") {
                statelink.innerHTML = " - ";
            } else {
                statelink.innerHTML = " + ";
            }
            statelink.href = 'javascript:stateClick(' + this.getId() + ')';
        } else {
            statelink.innerHTML = " . ";
        }
    
        nodeElement.appendChild(statelink);
    
        var namelink = window.document.createElement("a");
        namelink.id = "namelink_" + this.getId();
        namelink.href = 'javascript:nodeClick(' + this.getId() + ')';
        namelink.innerHTML = value.name;
        nodeElement.appendChild(namelink);
    
        if (state == "opened") {
            paintChildren();
        }
        if (parent.type == "nodesPane")
            nodeElement.style.left = 2;
        else
            nodeElement.style.left = 20;
        parent.appendChild(nodeElement);
    }
    

    }

    function Tree() {

    SkyObject.call(this);
    var children = [];
    var title = "title";
    var element = null;
    var parent = null;
    var value = null;
    var currentNodeId = -1;
    var preNodeId = -1;
    
    this.setCurrentNodeId = function(id) {
        preNodeId = currentNodeId;
        currentNodeId = id;
    }
    
    this.getPreNodeId = function() {
        return preNodeId;
    }
    
    this.getCurrentNodeId = function() {
        return currentNodeId;
    }
    
    this.getCurrentNode = function() {
        if (currentNodeId < 0) {
            return null;
        } else
            return getFromContainer(currentNodeId);
    }
    
    this.onNodeChange = function() {
    };
    
    this.bindData = function(data) {
        value = data;
    }
    
    this.addChild = function(data) {
        var node = new Node(data, this);
        children[children.length] = node;
        var treeElement = this.getElement();
        node.paint(treeElement);
    }
    
    this.addChildToSelectedNode = function(data) {
        var selectednode = this.getCurrentNode();
        var node = new Node(data, selectednode);
        selectednode.addChild(node);
    }
    
    this.removeChild = function(node) {
        var parent = node.getParentNode();
        if (parent.getId() == this.getId()) {
            var element = this.getElement();
            element.removeChild(node.getElement());
            var newChildren = [];
            var index = 0;
            for ( var i = 0; i < children.length; i++) {
                var child = children[i];
                if (node.getId() != child.getId()) {
                    newChildren[index] = child;
                    index++;
                }
            }
            delete children;
            children = newChildren;
    
        } else {
            parent.removeChild(node);
        }
    
    }
    
    this.getElement = function() {
        var nodesPane = window.document.getElementById("obj_" + this.getId())
        if (nodesPane == null) {
            nodesPane = window.document.createElement("div");
            nodesPane.id = "obj_" + this.getId();
            nodesPane.type = "nodesPane";
            if (value != null && value.length > 0) {
                for ( var i = 0; i < value.length; i++) {
                    var node = new Node(value[i], this);
                    children[i] = node;
                    node.paint(nodesPane);
                }
            }
        }
        return nodesPane;
    }
    
    this.paint = function(parent) {
        var nodesPane = window.document.createElement("div");
        nodesPane.id = "obj_" + this.getId();
        if (value != null && value.length > 0) {
            for ( var i = 0; i < value.length; i++) {
                var node = new Node(value[i], this);
                children[i] = node;
                node.paint(nodesPane);
            }
        }
        parent.appendChild(nodesPane);
    }
    

    }

    [/code]

    test.html
    [code="java"]

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


    tree.html



    var tree1 = null; var tree2 = null; window.onload = function() { var roots = { tree : [ { id : "1", name : "name1", children : [ { id : "10", name : "child0", children : [] }, { id : "11", name : "child1", children : [] }, { id : "12", name : "child2", children : [] }, { id : "13", name : "child3", children : [] }, { id : "14", name : "child4", children : [] } ] }, { id : "2", name : "name2", children : [] }, { id : "3", name : "name3", children : [] }, { id : "4", name : "name4", children : [] }, ] }; var parent1 = document.getElementById("tree1"); tree1 = new Tree(); tree1.bindData(roots.tree); tree1.paint(parent1); tree1.onNodeChange = function() { } var parent2 = document.getElementById("tree2"); tree2 = new Tree(); tree2.bindData(roots.tree); tree2.paint(parent2); tree2.onNodeChange = function() { } } function addNew() { var nodedata = { id : "123", name : "newNode123" }; tree1.addChild(nodedata); } function addNewToSelected() { var nodedata = { id : "123", name : "newNode123" }; tree1.addChildToSelectedNode(nodedata); } function removeSelectedNode() { var node = tree1.getCurrentNode(); tree1.removeChild(node); }


    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号