一個(gè)基于xml 得Ext treepanel

          Posted on 2008-12-30 11:23 Robert Su 閱讀(945) 評(píng)論(0)  編輯  收藏 所屬分類: ExtJS
          /**
              Create an Ext.tree.TreePanel in the passed Element using
              an XML document from the passed URL, calling the passed
              callback on completion.
              @param el {String/Element/HtmlElement} The tree's container.
              @param url {String} The URL from which to read the XML
              @param callback {function:tree.render} The function to call on completion,
                  defaults to rendering the tree.
          */
          function createXmlTree(el, url, callback) {
              
          var tree = new Ext.tree.TreePanel(el);
              
          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) {
          //    Text is nodeValue to text node, otherwise it's the tag name
              var t = ((XmlEl.nodeType == 3? XmlEl.nodeValue : XmlEl.tagName);

          //    No text, no node.
              if (t.replace(/\s/g,'').length == 0) {
                  
          return null;
              }
              
          var result = new Ext.tree.TreeNode({
                  text : t
              });

          //    For Elements, process attributes and children
              if (XmlEl.nodeType == 1) {
                  Ext.each(XmlEl.attributes, 
          function(a) {
                      
          var c = new Ext.tree.TreeNode({
                          text: a.nodeName
                      });
                      c.appendChild(
          new Ext.tree.TreeNode({
                          text: a.nodeValue
                      }));
                      result.appendChild(c);
                  });
                  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;
          }

          回頭有空添加詳細(xì)注解~~原文中6樓有更詳細(xì)的解答  http://extjs.com/forum/showthread.php?t=3987




          posts - 103, comments - 104, trackbacks - 0, articles - 5

          Copyright © Robert Su

          主站蜘蛛池模板: 九龙坡区| 荔浦县| 库尔勒市| 靖西县| 明溪县| 壶关县| 潼关县| 报价| 上饶市| 来宾市| 遂宁市| 大港区| 基隆市| 兴城市| 湖北省| 沙坪坝区| 秭归县| 桂林市| 绍兴县| 山东省| 金山区| 永吉县| 霍城县| 仲巴县| 汝阳县| 清河县| 泸水县| 全南县| 本溪| 焦作市| 宾川县| 泰宁县| 万山特区| 苏州市| 宁阳县| 安岳县| 辽宁省| 泉州市| 天柱县| 和静县| 孟村|