xTree的用法
(创建时间:2010年05月15日 20:34:00)
Jangogo :
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">
- //xTree(sRootLabel, sIconSrc, hideColumnHeadings, hideRootNode)
- tree = new xTree('Test Tree', '',true,false);
- tree.addColumn ( new xTreeColumn(0, '') ); //隐藏列
- tree.addColumn ( new xTreeColumn(440, '') );
- //tree.showColumnHeadings=false;
- tree.nLabelColumn = 1; //第一列作为标签文字列
- tree.BodyHeight=320;
- tree.rootNode.setText('','科目'); //一定会有有一个跟节点,参数的数量就是节点文字按列的数量
- //可以自己定义一个节点,非绑定,一般而言,这个节点在我们动态和XML绑定时是必须的,因为动态载入时,要求有一个Loading节点,载入完成后会自动删除
- var node = new xTreeNode(true);
- node.setText('','Loading.');
- tree.add(node);
- //用XML内容载入子节点(动态绑定XML)
- 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[子节点2的第一列]]></C>
- <C><![CDATA[子节点2的第二列]]></C>
- <C>
- <A>
- <C><![CDATA[子节点2.1的第一列]]></C>
- <C><![CDATA[子节点2.1的第二列]]></C>
- <C><![CDATA[子节点2.1的子子节点(必须的)]]></C>
- </A>
- <A>
- <C><![CDATA[子节点2.2的第一列]]></C>
- <C><![CDATA[子节点2.2的第一列]]></C>
- <C><![CDATA[子节点2.2的第一列]]></C>
- </A>
- </C>
- </A>
树的定义:
- function xTree(sRootLabel, sIconSrc, hideColumnHeadings, hideRootNode) {
- this.ID = xTreeHandler.getUniqueID();
- xTreeHandler.allTrees[this.ID] = this;
- this.all = new Array(); //user indices
- this.allNodes = new Array(); //system indices
- 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 is the column in which the icons and branches are drawn
- 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) {
- //refKey is an optional user-specified value which will be matched to this node for easy reference later
- 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; //set to false once children are loaded to prevent sXMLSrc being used to dynamically load the children
- this.bShowHandle = true; //this is the + or - icon
- 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; //新增加的属性
- //this.className = 'tlc_node_row';
- //this.classNameHover = 'tlc_node_row_hover';
- //this.classNameClick = 'tlc_node_row_click';
- }
XML node 属性和treenode的对应关系
- <A
- ondblclick='sel();'
- onclick='rpt();'"
- showchildren="yes"
- iconsrc="images/folder.gif"
- xmlsrc="myfunCanReturnXML()"
- ><![CDATA[ node text ]]></A>
文档中心