Ext树工具类 Jsontree和Xmltree

使用说明:
使用本工具类,可以快速从xml和json文件中创建EXT树.
预览:
1213758627-clip-2kb.png
tree.html:

None.gif < SCRIPT src = " js/jsontree.js "  type = text / javascript>< / SCRIPT >
None.gif
< SCRIPT src = " js/xmltree.js "  type = text / javascript>< / SCRIPT >
None.gif
< / HEAD>
None.gif
< BODY >
None.gif
< div id = " jsontree " >< / div>
None.gif
< div id = " xmltree " >< / div>
None.gif
< / BODY>
None.gif
< / HTML>
None.gif
< script type = " text/javascript " >
None.gifExt.BLANK_IMAGE_URL 
=   ' js/ext-2.0/resources/images/default/s.gif ' ;
ExpandedBlockStart.gifContractedBlock.gif
var  jsontree  =   new  createJsonTree( " jsontree " " tree.json " function ()  dot.gif {
InBlock.gif    jsontree.render();
InBlock.gif    
this.getRootNode().expand();
ExpandedBlockEnd.gif}
);
None.gif
ExpandedBlockStart.gifContractedBlock.gif
var  menuTree  =   new  createXmlTree( " xmltree " " tree.xml " function ()  dot.gif {
InBlock.gif    menuTree.render();
InBlock.gif    
this.getRootNode().expand();
ExpandedBlockEnd.gif}
);
None.gif
< / script>
None.gif

jsontree.js:
ExpandedBlockStart.gif ContractedBlock.gif function  createJsonTree(el, url, callback)  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var tree = new Ext.tree.TreePanel(dot.gif{
InBlock.gif        el : el,
InBlock.gif        animate : 
true,
InBlock.gif        border : 
false,
InBlock.gif        autoHeight : 
true,
InBlock.gif        rootVisible : 
true
ExpandedSubBlockEnd.gif    }
);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var treeNodesFromArray = function(parentNode, children) dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        Ext.each(children, 
function(child) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
var node = new Ext.tree.TreeNode(dot.gif{
InBlock.gif                text : child.text,
InBlock.gif                url : child.url,
InBlock.gif                color : child.color,
InBlock.gif                menuid : child.menuid,
InBlock.gif                expanded : child.expanded
ExpandedSubBlockEnd.gif            }
);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (!parentNode) dot.gif{
InBlock.gif                tree.setRootNode(node);
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 else dot.gif{
InBlock.gif                parentNode.appendChild(node);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (child.children) dot.gif{
InBlock.gif                treeNodesFromArray(node, child.children);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
this);
ExpandedSubBlockEnd.gif    }
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
var proxy = new Ext.data.HttpProxy(dot.gif{
InBlock.gif            url : url
ExpandedSubBlockEnd.gif        }
);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        proxy.on(
"loadexception"function(o, response, e) dot.gif{
InBlock.gif            
if (e)
InBlock.gif                
throw e;
ExpandedSubBlockEnd.gif        }
);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        proxy.load(
nulldot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            read : 
function(response) dot.gif{
InBlock.gif                
var treeJson = Ext.util.JSON.decode(response.responseText);
InBlock.gif                treeNodesFromArray(
null, treeJson);
InBlock.gif                callback.call(tree);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
function() dot.gif{
ExpandedSubBlockEnd.gif        }
this);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch (e) dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return tree;
ExpandedBlockEnd.gif}

None.gif

xmltree.js
ExpandedBlockStart.gif ContractedBlock.gif function  loadXMLext(xmlStr)  dot.gif {
InBlock.gif
InBlock.gif    
if (!xmlStr)
InBlock.gif        
return null;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (window.ActiveXObject) dot.gif{
InBlock.gif        
var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 else dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (document.implementation && document.implementation.createDocument) dot.gif{
InBlock.gif            
var xmlDom = document.implementation
InBlock.gif                    .createDocument(
"""doc"null)
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    xmlDom.async 
= true;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try dot.gif{
InBlock.gif        xmlDom.loadXML(xmlStr);
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch (e) dot.gif{
InBlock.gif        
var oParser = new DOMParser();
InBlock.gif        xmlDom 
= oParser.parseFromString(xmlStr, "text/xml");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return xmlDom;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
function  createXmlTree(el, xmlsrc, callback)  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var tree = new Ext.tree.TreePanel(dot.gif{
InBlock.gif        el : el,
InBlock.gif        animate : 
true,
InBlock.gif        border : 
false,
InBlock.gif        autoHeight : 
true,
InBlock.gif        rootVisible : 
true
ExpandedSubBlockEnd.gif    }
);
InBlock.gif
InBlock.gif    
var xmlDom = loadXMLext(xmlsrc);
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try dot.gif{
InBlock.gif        tree.setRootNode(treeNodeFromXml(xmlDom.documentElement 
|| xmlDom));
InBlock.gif        callback.call(tree);
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch (e) dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
var p = new Ext.data.HttpProxy(dot.gif{
InBlock.gif            url : xmlsrc
ExpandedSubBlockEnd.gif        }
);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        p.on(
"loadexception"function(o, response, e) dot.gif{
InBlock.gif            alert(
"loadException");
ExpandedSubBlockEnd.gif        }
);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        p.load(
nulldot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            read : 
function(response) dot.gif{
InBlock.gif                
var doc = response.responseXML;
InBlock.gif                tree.setRootNode(treeNodeFromXml(doc.documentElement 
|| doc));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
, callback, tree);
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return tree;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
function  treeNodeFromXml(XmlEl)  dot.gif {
InBlock.gif
InBlock.gif    
var t = ((XmlEl.nodeType == 3? XmlEl.nodeValue : XmlEl.tagName);
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (t.replace(/\s/g, '').length == 0dot.gif{
InBlock.gif        
return null
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
var result = dot.gif{
InBlock.gif        text : t
ExpandedSubBlockEnd.gif    }
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (XmlEl.nodeType == 1dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        Ext.each(XmlEl.attributes, 
function(a) dot.gif{
InBlock.gif            
if (a.nodeName == "href" && XmlEl.hasChildNodes())
InBlock.gif                
return;
InBlock.gif            result[a.nodeName] 
= a.nodeValue;
ExpandedSubBlockEnd.gif        }
);
InBlock.gif
InBlock.gif        result 
= new Ext.tree.TreeNode(result);
ExpandedSubBlockStart.gifContractedSubBlock.gif        Ext.each(XmlEl.childNodes, 
function(el) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ((el.nodeType == 1|| (el.nodeType == 3)) dot.gif{
InBlock.gif                
var c = treeNodeFromXml(el);
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (c) dot.gif{
InBlock.gif                    result.appendChild(c);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return result;
ExpandedBlockEnd.gif}

示例数据tree.json:
ExpandedBlockStart.gif ContractedBlock.gif [ dot.gif {
InBlock.gif    text : 
'json树根',
InBlock.gif    url : 
'root',
InBlock.gif    menuid : 
'root',
InBlock.gif    expanded : 
true,
ExpandedSubBlockStart.gifContractedSubBlock.gif    children : [
dot.gif{
InBlock.gif        text : 
'json节点一',
InBlock.gif        url : 
'001',
InBlock.gif        menuid : 
'01',
InBlock.gif        expanded : 
true
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
dot.gif{
InBlock.gif        text : 
'json节点二二',
InBlock.gif        expanded : 
true,
ExpandedSubBlockStart.gifContractedSubBlock.gif        children : [
dot.gif{
InBlock.gif            text : 
'json节点三',
InBlock.gif            url : 
'003',
InBlock.gif            menuid : 
'03',
InBlock.gif            expanded : 
true
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
dot.gif{
InBlock.gif            text : 
'json节点四',
InBlock.gif            url : 
'004',
InBlock.gif            menuid : 
'04',
InBlock.gif            expanded : 
true
ExpandedSubBlockEnd.gif        }
]
ExpandedSubBlockEnd.gif    }
]
ExpandedBlockEnd.gif}
]
示例数据tree.xml:
None.gif < xml 树根 expanded ="true"  menuid ="10000"   >
None.gif  
< xml 节点一 expanded ="true"  menuid ="10005"   />  
None.gif  
< xml 节点二 expanded ="true"  menuid ="10007"    />  
None.gif
</ xml树根 >









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值