前面发了一个 html to tree 再发一个 xml to tree

以下为代码
010 | var XML2Tree = function (ini) { |
011 | var $ = document, _this = this , $$ = 'documentElement' ; |
012 | this .getTitle = ini.getTitle || String; |
013 | this .click = ini.click || String; |
014 | this .box = ini.shell.big ? $.getElementById(ini.shell) : ini.shell; |
015 | this .edit = ini.edit ? true : false ; |
016 | this .color = ini.color ? ini.color : '#v' ; |
017 | this .row = ini.row ? ini.row : '' ; |
018 | this .box.innerHTML = '<p style="margin-left:10px"><img src="/load3.gif" /> loading...<p>' ; |
019 | this .getValue = ini.getValue || String; |
021 | this .xml = !!$.all ? ( new ActiveXObject( 'Microsoft.XMLDOM' )) : ( new XMLHttpRequest()); |
022 | this .xml.onreadystatechange = function () { |
023 | if (_this.xml.readyState == 4) { |
024 | _this.box.innerHTML = '' ; |
025 | _this.addSub($.all ? _this.xml[$$] : _this.xml.responseXML[$$], _this.box) |
029 | this .xml.async = true ; this .xml.load(ini.url) |
032 | this .xml.open( "GET" , ini.url, true ); this .xml.send( null ) |
038 | XML2Tree.prototype = { |
039 | folder: function (node) { |
040 | var UI = function (_) { return document.createElement(_) } |
041 | , caption = UI( 'DT' ), shell = UI( 'DL' ), body = UI( 'DD' ), $ = XML2Tree.ini; |
042 | shell.appendChild(caption); |
043 | shell.appendChild(body); |
044 | var folderIco = this .selectIco($.folder, $.node, XML2Tree.hasChild(node)); |
045 | caption.innerHTML = ( this .edit ? '<label>' + this .expand( this .getValue.call(node)) + '</label>' : '' ) |
046 | + this .getLineIco(node) + this .getHasIco(node) |
048 | + this .getTitle.call(node) + '</span></a>' ; |
049 | caption.mapNode = node; |
051 | caption.onmouseover = function () { |
052 | this .style.backgroundColor = co; |
054 | caption.onmouseout = function () { |
055 | this .style.backgroundColor = '' ; |
057 | return { 'shell' : shell, 'caption' : caption }; |
059 | addSub: function (node, shell) { |
060 | if (node == null ) return ; |
061 | var nodes = node.childNodes, _tree = this ; |
062 | for ( var i = 0; i < nodes.length; i++) { |
063 | if (nodes[i].nodeType != 1) continue ; |
064 | var _ = this .folder(nodes[i]); |
065 | shell.appendChild(_.shell); |
066 | _.caption.onclick = function (e) { |
067 | var $ = XML2Tree, childShell = $.next( this ); |
069 | var wrap = this .parentNode.getElementsByTagName( 'DD' )[0]; |
070 | if (XML2Tree.hasChild( this .mapNode)) { |
071 | _tree.addSub( this .mapNode, wrap); |
076 | if (!childShell) return ; |
077 | if (XML2Tree.hasChild(childShell)) { |
078 | var hide = childShell.style.display == 'none' ; |
079 | childShell.style.display = hide ? '' : 'none' ; |
080 | $.toggle( this , hide); |
083 | e = e || window.event; |
084 | var sE = e.srcElement || e.target; |
085 | if (sE.tagName.toUpperCase() == 'SPAN' ) { |
086 | var title = sE.innerHTML; |
087 | if (!XML2Tree.hasChild(childShell)) { |
088 | _tree.click.call(sE, title, false ); |
090 | _tree.click.call(childShell, title, true ); |
096 | getLineIco: function (node) { |
097 | var icos = [], root = node.ownerDocument; |
098 | if (!root) return null ; |
099 | node = node.parentNode; |
100 | while (node.parentNode != root) { |
101 | var $ = XML2Tree, img = this .selectIco($.ini.line, $.ini.empty, !!$.next(node)); |
102 | icos = [img].concat(icos); |
103 | node = node.parentNode; |
105 | return icos.join( '' ); |
107 | getHasIco: function (node) { |
108 | var last = !!XML2Tree.next(node), $ = XML2Tree.ini; |
109 | return XML2Tree.hasChild(node) ? |
110 | this .selectIco($.plus, $.plusBottom, last) : |
111 | this .selectIco($.join, $.joinBottom, last) |
113 | expand: function (i) { |
114 | var r = this .row, s = []; |
115 | for ( var k = 0; k < r.length; k++) { |
116 | var url = r[k][0].replace( '{0}' , i); |
117 | s.push( '<a href="' + url + '">' + r[k][1] + '</a>' + (k == r.length - 1 ? '' : ' | ' )); |
121 | selectIco: function (_1, _2, bool) { |
122 | return '<img src="' + (bool ? _1 : _2) + '" align="absimddle" />' |
130 | folder: 'ui/folder.gif' , |
131 | folderOpen: 'ui/folderopen.gif' , |
133 | empty: 'ui/empty.gif' , |
136 | joinBottom: 'ui/joinbottom.gif' , |
138 | plusBottom: 'ui/plusbottom.gif' , |
139 | minus: 'ui/minus.gif' , |
140 | minusBottom: 'ui/minusbottom.gif' , |
141 | nlPlus: 'ui/nolines_plus.gif' , |
142 | nlMinus: 'ui/nolines_minus.gif' |
145 | XML2Tree.prevLoad = function () { |
146 | for ( var key in this .ini) { |
148 | $.src = this .ini[key]; |
151 | XML2Tree.next = function (node) { |
152 | var $ = node, _ = 'nextSibling' ; |
153 | for ($ = $[_]; $; $ = $[_]) { |
154 | if ($.nodeType == 1) { return $ } |
158 | XML2Tree.hasChild = function (node) { |
159 | var $ = node.childNodes; |
160 | for ( var i = 0; i < $.length; i++) |
161 | if ($[i].nodeType == 1) return true ; |
164 | XML2Tree.toggle = function (node, isOpen) { |
165 | var imgs = node.getElementsByTagName( 'IMG' ) |
166 | , f = imgs.length - 1, $ = XML2Tree.ini; |
167 | imgs[f].src = isOpen ? $.folderOpen : $.folder; |
168 | if ( this .next(node.parentNode)) { |
169 | imgs[f - 1].src = isOpen ? $.minus : $.plus; |
171 | imgs[f - 1].src = isOpen ? $.minusBottom : $.plusBottom; |
HTML代码
1 | < div id = "TreeViewTreeView1" ></ div > |
4 | < div id = "TreeView1" ></ div > |
JS调用
06 | row: [[ 'city.aspx?action=add&i={0}&Language=Chinese' , '添加' ], [ 'city.aspx?action=edit&i={0}&Language=Chinese' , '修改' ], [ 'city.aspx?action=delete&i={0}&Language=Chinese' , '删除' ]], |
07 | getTitle: function () { return this .getAttribute( 't' ) }, |
08 | getValue: function () { return this .getAttribute( 'v' ) }, |
09 | click: function (title, isFolder) { |
下载地址
点我下载
~!~~~ 民工的命
原创代码,转载请联系 柴哥!!!
|