[Ext扩展]JsonTreeLoader:一次加载所有树节点更新1.1版本

仿照官网例子:XmlTreeLoader做的,下面有实例
注意:html文件的库引用请自行解决

Js代码 复制代码
  1. /**  
  2.  * 通过读取JSON串生成树的层次结构,务必保证json数组中结点出现次序与树完全展开时一致!  
  3.  * (通过后台对编码进行排序来完成)  
  4.  * JSON串根节点默认为'list'  
  5.  * 父结点id字段为  
  6.  *   
  7.  * 注意:  
  8.  * 1.因为是一次加载全部结点,节点数过多的话将严重影响性能,此时请使用异步加载!  
  9.  * 2.结点的id属性对应json中code属性,结点text属性对应json中name属性。  
  10.  * 3.根节点的直接子节点的parentcode字段为null或者空字符串都可以  
  11.  *   
  12.  * v1.1改动:  
  13.  * 1.不再需要leaf属性!一个结点是否为叶子结点将基于json中下一个结点是否为其子节点来判断!  
  14.  * 本特性将提高转变叶子节点为父节点的操作效率,此时你在界面上给一个叶子结点添加一个子节点  
  15.  * 不用再修改叶子结点在后台的leaf属性,你要做的只是保证返回json中的编码规范以及出现次序。  
  16.  * 2.如果你设置了leaf属性则此属性将被忽略  
  17.  *   
  18.  * @author chemzqm@gmail.com  
  19.  *   
  20.  */  
  21. Ext.ns('Ext.ux.tree');   
  22.   
  23.   
  24. Ext.ux.tree.JsonTreeLoader = Ext.extend(Ext.tree.TreeLoader, {   
  25.     root: 'list',   
  26.     paramName: {   
  27.         parentcode: 'parentcode',   
  28.         id: 'code'  
  29.     },   
  30.        
  31.     // private override   
  32.     processResponse: function(response, node, callback){   
  33.         var json = response.responseText;   
  34.         var array = Ext.decode(json)[this.root];        
  35.         try {   
  36.             node.beginUpdate();   
  37.             node.appendChild(this.parseArray(array));   
  38.             node.endUpdate();   
  39.                
  40.             if (typeof callback == "function") {   
  41.                 callback(this, node);   
  42.             }   
  43.         }    
  44.         catch (e) {   
  45.             this.handleFailure(response);   
  46.         }   
  47.     },   
  48.        
  49.     // private   
  50.     parseArray: function(array){   
  51.         var pnodes = [];   
  52.         var nodes = [];   
  53.         for (var i = 0; i < array.length; i++) {   
  54.             var o =  array[i];//判断是否叶子结点   
  55.             if(array[i+1]&&array[i+1][this.paramName.parentcode]==o[this.paramName.id]){   
  56.                 o.leaf = false;   
  57.             }else{   
  58.                 o.leaf = true;   
  59.             }   
  60.             var treeNode = this.createNode(o);   
  61.             if (!o[this.paramName.parentcode]) {   
  62.                 nodes.push(treeNode);   
  63.             }   
  64.             else {   
  65.                 for (var j = pnodes.length - 1; j >= 0; j--) {   
  66.                     if (pnodes[j].id == o[this.paramName.parentcode]) {   
  67.                         pnodes[j].appendChild(treeNode);   
  68.                         break;   
  69.                     }   
  70.                 }   
  71.             }   
  72.             if (!treeNode.leaf) {   
  73.                 pnodes.push(treeNode);   
  74.             }   
  75.                
  76.         }   
  77.         return nodes;   
  78.     },   
  79.     // private override node的id是json里面的code字段   
  80.     createNode: function(o){       
  81.         var attr = {   
  82.             id: o.code,   
  83.             text: o.name   
  84.         };   
  85.         Ext.applyIf(attr, o);   
  86.         attr.loaded = true;   
  87.            
  88.         this.processAttributes(attr);   
  89.            
  90.         return Ext.ux.tree.JsonTreeLoader.superclass.createNode.call(this, attr);   
  91.     },   
  92.        
  93.     /*  
  94.      * Template method intended to be overridden by subclasses that need to provide  
  95.      * custom attribute processing prior to the creation of each TreeNode.  This method  
  96.      * will be passed a config object containing existing TreeNode attribute name/value  
  97.      * pairs which can be modified as needed directly (no need to return the object).  
  98.      */  
  99.     processAttributes: Ext.emptyFn   
  100. });   
  101.   
  102. //backwards compat   
  103. Ext.ux.JsonTreeLoader = Ext.ux.tree.JsonTreeLoader;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值