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

          Posted on 2008-12-30 11:23 Robert Su 閱讀(940) 評(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

          主站蜘蛛池模板: 南澳县| 边坝县| 信宜市| 塔城市| 稻城县| 五寨县| 铁力市| 会昌县| 盘锦市| 环江| 施秉县| 筠连县| 定远县| 南开区| 泸定县| 瑞金市| 宝鸡市| 凭祥市| 沿河| 德兴市| 陇川县| 兴安盟| 青铜峡市| 光泽县| 申扎县| 南投市| 霍林郭勒市| 左权县| 抚顺县| 望江县| 习水县| 宜兰市| 迁安市| 白城市| 沈阳市| 房产| 博客| 子长县| 咸丰县| 沭阳县| 印江|