xTree的用法
jangogo @ 2010-5-15 12:34:00
js/xtree.js 所提供的树控件是一个可以和XML绑定的树图控件(默认是不绑定的, 通过Node的loadXMLDoc 等方法和XML绑定并载入XML内容。
依赖文件:
js文件:
<script language=javascript src="js/xtree.js"></script>
CSS文件
<link href="skins/default/tree.css" rel="stylesheet" type="text/css">
tree = new xTree( 'Test Tree' , '' , true , false );
tree.addColumn ( new xTreeColumn(0, '' ) );
tree.addColumn ( new xTreeColumn(440, '' ) );
tree.nLabelColumn = 1;
tree.BodyHeight=320;
tree.rootNode.setText( '' , '科目' );
var node = new xTreeNode( true );
node.setText( '' , 'Loading.' );
tree.add(node);
tree.rootNode.loadXMLDoc(xDoc);
XML的格式:节点的TagName是不重要的,任何TagName都是同样的结果,关键是节点和子节点的数量以及结构
节点值必须采用<![CDATA[ ]]>包裹
< A >
< C ><![CDATA[ 子节点1的第一列]]> </ C >
< C ><![CDATA[ 子节点1的第二列]]> </ C >
< C ><![CDATA[ 子节点1的子子节点(必须的)]]> </ C >
</ A >
< A >
< C ><![CDATA[ 子节点1的第一列]]> </ C >
< C ><![CDATA[ 子节点1的第二列]]> </ C >
< C >
< C ><![CDATA[ 子节点1.1的第一列]]> </ C >
< C ><![CDATA[ 子节点1.1的第二列]]> </ C >
< C ><![CDATA[ 子节点1.1的子子节点(必须的)]]> </ C >
</ C >
</ A >
树的定义:
function xTree(sRootLabel, sIconSrc, hideColumnHeadings, hideRootNode) {
this .ID = xTreeHandler.getUniqueID();
xTreeHandler.allTrees[ this .ID] = this ;
this .all = new Array();
this .allNodes = new Array();
this .columns = new Array();
this .rootNode = new xTreeNode( true , sIconSrc, null , 1);
this .rootNode.columnText[0] = sRootLabel;
this .rootNode.depth = 0;
this .rootNode.bShowHandle = false ;
this .rootNode.oTree = this ;
this .allNodes[ this .rootNode.ID] = this .rootNode;
this .rendered = false ;
this .nLabelColumn = 0;
this .iconPath = 'images/' ;
this .showColumnHeadings = hideColumnHeadings ? false : true ;
this .showRootNode = hideRootNode ? false : true ;
this .disableRecalc = false ;
this .enableNodeDragdrop = false ;
this .dragDrop = null ;
this .selectedNodeID= null ;
this .BodyHeight=400;
this .isfocus= false ;
}
节点定义
function xTreeNode(bShowChildren, sIconSrc, sXMLSrc, refKey) {
this .columnText = new Array();
this .bShowChildren = bShowChildren ? true : false ;
this .sIconSrc = sIconSrc ? sIconSrc : null ;
this .sXMLSrc = ( typeof (sXMLSrc) == 'string' ) ? sXMLSrc : '' ;
this .bDynamicNode = this .sXMLSrc.length > 0 ? true : false ;
this .bShowHandle = true ;
this .refKey = (refKey || refKey == 0) ? refKey : null ;
this .ID = xTreeHandler.getUniqueID();
this .sImages = '' ;
this .sHandle = '' ;
this .onmouseenter = '' ;
this .onmouseleave = ''
this .onclick = '' ;
this .ondblclick = '' ;
this .useIcon = true ;
this .nextSibling = null ;
this .previousSibling = null ;
this .firstChild = null ;
this .lastChild = null ;
this .parentNode = null ;
this .oTree = null ;
this .rendered = false ;
this .xml = null ;
}
XML node 属性和treenode的对应关系
< A
ondblclick = 'sel();'
onclick = 'rpt();' "
showchildren = "yes"
iconsrc = "images/folder.gif"
xmlsrc = "myfunCanReturnXML()"
> <![CDATA[ node text ]]> </ A >