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

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

          主站蜘蛛池模板: 乐都县| 临泽县| 凌源市| 湘阴县| 泸西县| 建阳市| 库尔勒市| 宁乡县| 林口县| 海盐县| 招远市| 西充县| 葵青区| 繁峙县| 将乐县| 九龙坡区| 平罗县| 花莲县| 老河口市| 绥化市| 观塘区| 北安市| 壤塘县| 樟树市| 洱源县| 株洲市| 车致| 中山市| 科技| 中宁县| 扎兰屯市| 武平县| 宜宾市| 辽阳市| 富顺县| 青河县| 海伦市| 靖宇县| 虞城县| 华容县| 湘阴县|