1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
<%@ Page Language="vb" AutoEventWireup="false"%>
<html>
<head>
<title>Test Tree Xml</title>
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="/lib/_extjs3/resources/css/ext-all.css" />
<style type="text/css">
html, body {
font:normal 12px georgia;
margin:0;
padding:0;
border:0 none;
overflow:hidden;
height:100%;
}
</style>
<!-- ENDCSS -->
<!-- LIBSJS -->
<script type="text/javascript" src="/lib/_extjs3/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="/lib/_extjs3/ext-all.js"></script>
<script type="text/javascript" src="/lib/extjs/src/data/HttpProxy.js"></script>
<!-- ENDLIBSJS -->
<script type="text/javascript">
Ext.onReady(function(){
var TreeTest = function(){
// shorthand
var Tree = Ext.tree;
function createXmlTree(url, callback)
{
// yui-ext tree
var tree = new Tree.TreePanel({
el:'tree',
animate:true,
autoScroll:true,
loader: new Tree.TreeLoader(),
enableDD:true,
containerScroll: true,
dropConfig: {appendOnly:true}
});
// add a tree sorter in folder mode
new Tree.TreeSorter(tree, {folderSort:true});
// set the root node
var root = new Tree.AsyncTreeNode({
text: 'Ext JS',
draggable:false, // disable root node dragging
id: 'source'
});
//tree.setRootNode(root);
// render the tree
//tree.render();
//root.expand(false, /*no anim*/ false);
var p = new Ext.data.HttpProxy({url:url});
p.on("loadexception", function(o, response, e)
{
if (e) throw e;
});
p.load(null,
{
read: function(response)
{
var doc = response.responseXML;
tree.setRootNode(treeNodeFromXml(doc.documentElement || doc));
}
}, callback || tree.render, tree);
return tree;
}
/**
Create a TreeNode from an XML node
*/
function treeNodeFromXml(XmlEl)
{
var t = '';
if (XmlEl.tagName == undefined ) {
return null;
}
if (t.replace(/s/g,'').length == 0) {
//Text is nodeValue to text node, otherwise it's the tag name
t = ((XmlEl.nodeType == 3) ? XmlEl.nodeValue.replace(/s/g,'') : XmlEl.tagName);
}
//No text, no node.
if (t.replace(/s/g,'').length == 0) {
return null;
}
// Special case of an element containing no attributes and just one text node
var leafTextNode = ((XmlEl.attributes.length == 0) && (XmlEl.childNodes.length == 1) && (XmlEl.firstChild.nodeType == 3));
if (leafTextNode){
return new Ext.tree.TreeNode({
tagName: XmlEl.tagName,
text: XmlEl.firstChild.nodeValue
});
}
var result = new Ext.tree.TreeNode({
text : t
});
//For Elements, process attributes and children
if (XmlEl.nodeType == 1 && XmlEl.tagName!= 'Node') { //tagName = nom de noeud principal
Ext.each(XmlEl.attributes, function(a) {
var c = new Ext.tree.TreeNode({
text: a.nodeName //nom noeuds principaux
});
if (a.nodeName == 'Champs' || a.nodeName == 'Type') {
return null;
}
c.appendChild(new Ext.tree.TreeNode({
text: a.nodeValue //nom feuille
}));
result.appendChild(c);
});
if (!leafTextNode) {
Ext.each(XmlEl.childNodes, function(el) {
//Only process Elements and TextNodes
if ((el.nodeType == 1) || (el.nodeType == 3)) {
var c = treeNodeFromXml(el);
if (c) {
result.appendChild(c);
}
}
});
}
}
return result;
}
return {
init : function(){
var tree1 = new createXmlTree('fiche.xml', function(){
this.render();
this.getRootNode().expand();
});
}
};
}();
Ext.EventManager.onDocumentReady(TreeTest.init, TreeTest, true);
});
</script>
</head>
<body>
<div id="tree"></div>
</body>
</html> |
Partager