---
title: "今天下午刚写的树形控件"
author: "micrexp"
date: "2007-05-11T19:41:38Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-05-11T19:41:38Z"
id: 78945
url: "https://micrexp.iteye.com/blog/78945"
---

```

var HK_TreeNode = function(pNode){
    var me = this;
    var nodeValue = "";
    var nodeImage = document.createElement("img");
    var nodeElement = document.createElement("div");
    var nodeText = document.createElement("div");
    var childLayout = document.createElement("div");    
    nodeElement.appendChild(nodeImage);
    nodeElement.appendChild(nodeText);
    nodeElement.appendChild(childLayout);
    nodeImage.src="images/plus.png";
    nodeImage.className = "tree_nodeimage";
    nodeText.className = "tree_nodetext";
    childLayout.className = "tree_nodelayout";
    childLayout.style.display = "none";
    var nodeIndex = -1;
    var parentNode = null; 
    var level = 0;
    var items = new Array();
    if(parentNode!=null){
        level = pNode.level + 1;
        parentNode = pNode; 
    }
    this.tree = null;
    this.data = null;
    this.setActive = function(){
        var activeNode = this.tree.activeNode;
        if(activeNode!=null){
            activeNode.deActive();        
        }
        nodeText.className = "tree_nodetextActive";
        this.tree.activeNode = this;
    }
    this.deActive = function(){
        this.tree.activeNode = null;    
        nodeText.className = "tree_nodetext";
    }
    this.appendChild = function(node){
        nodeIndex = items.push(node);
        childLayout.appendChild(node.getElement()); 
        nodeImage.src="images/plus.gif";
    }
    this.expand = function(){
        if(childLayout.style.display != "none"){
                nodeImage.src="images/plus.gif";
                childLayout.style.display = "none";
        }else{
            if(items.length>0){
                nodeImage.src="images/plusExpand.gif";
                childLayout.style.display = "";
            }
        }
    }
    this.getCount = function(){
        return items.length;
    }    
    this.getLevel = function(){
        return level;
    }
    this.setParentNode = function(node){
        parentNode = node;
        node.appendChild(nodeElement);
    }
    this.getParentNode = function(){
        return node;
    }
    this.getElement = function(){
        return nodeElement;
    }
    this.getImageElement = function(){
        return nodeImage;
    }
    this.getTextElement = function(){
        return nodeText;
    }
    this.setText = function(value){
        nodeText.innerText = value;
    }
    this.getText = function(){
        return nodeText.innerText;
    }
    this.setValue = function(value){
        nodeValue = value;
        this.setText(value);
    }
    this.getValue = function(){
        return value;
    }    
    function onnodeclick(){
        me.expand();
    }
    function onnodetextclick(){
        me.setActive();
    }
    function ontextdblclick(){
        me.expand();
    }
    function onselect(){
        return false;
    }
    nodeElement.attachEvent("onselectstart",onselect);
    nodeText.attachEvent("onclick",onnodetextclick);
    nodeText.attachEvent("ondblclick",ontextdblclick);
    nodeImage.attachEvent("onclick",onnodeclick);
}

var HK_Tree = function(){
    var me = this;
    var layout = document.createElement("div");
    var items = new Array();
    var map = new Object();
    var root = new Array();
    var activeNode = null;    
    this.xml = null;
    this.parent = null;
    this.primaryField = "";
    this.parentField = "";
    this.valueField = "";
    this.textField = "";
    this.activeNode = null;
    this.appendNode = function(parentNode){
        var node = new HK_TreeNode(parentNode);
        if(parentNode!=null){
            parentNode.appendChild(node);
        }else{
            layout.appendChild(node.getElement());
            root.push(node);
        }
        node.tree = this;
        items.push(node);        
        return node;
    }
    this.getNodeByIndex = function(index){
        if(items[i]!=undefined){
            return items[i];
        }
        return null;
    }
    this.getNodeByName = function(name){
        if(map[name]!=undefined){
            return map[name];
        }
        return null;        
    }
    this.getCount = function(){
        return items.length;
    }
    this.laodxml = function(){
        
    }
    this.databand = function(){
        var oRows = this.xml.getElementsByTagName("Row");
        var oCol,oCells,oRow;
        for(var i=0;i<oRows.length;i++){
            var parentId = oRows[i].selectSingleNode("./field[@name='"+this.parentField+"']");
            var nodeId = oRows[i].selectSingleNode("./field[@name='"+this.primaryField+"']");
            var nodeValue = oRows[i].selectSingleNode("./field[@name='"+this.valueField+"']");
            var nodeText = oRows[i].selectSingleNode("./field[@name='"+this.textField+"']");
            if(parentId!=null&&nodeId!=null){
                var node = this.appendNode(this.getNodeByName(parentId.text));
                node.data = oRows[i].xml;
                if(nodeValue!=null){
                    node.setValue(nodeValue.text);                    
                }
                if(nodeText!=null){
                    node.setText(nodeText.text);
                }
                map[nodeid.text] = node;
            }
            
        }
        this.ondataband();
    }
    this.init = function(){
        this.parent.appendChild(layout);
    }
    this.ondataband = function(){};
}

var tree = new HK_Tree();
tree.parent = document.getElementById("treepanel");
tree.primaryField = "unitId";
tree.parentField = "parentUnitId";
tree.valueField = "name";
tree.textField = "name";
tree.init();

```

今天下午完成的,发上来给大家看看