extjs 之Tree(columnTree) to jsonstring

简介:
treeSerializer.js
 
/* tostring.js */ 

/** 
* Returns a string of Json that represents the tree 
* @param {Function} (optional) A function, which when passed the node, returns true or false to include 
* or exclude the node. 
* @param {Function} (optional) A function, which when passed an attribute name, and an attribute value, 
* returns true or false to include or exclude the attribute. 
* @return {String} 
*/
 
Ext.tree.TreePanel.prototype.toJsonString =  function(nodeFilter, attributeFilter, attributeMapping){ 
             return  this.getRootNode().toJsonString(nodeFilter, attributeFilter, attributeMapping); 
}; 

/** 
* Returns a string of Json that represents the node 
* @param {Function} (optional) A function, which when passed the node, returns true or false to include 
* or exclude the node. 
* @param {Function} (optional) A function, which when passed an attribute name, and an attribute value, 
* returns true or false to include or exclude the attribute. 
* @return {String} 
*/
 
Ext.tree.TreeNode.prototype.toJsonString =  function(nodeFilter, attributeFilter, attributeMapping){ 
//  Exclude nodes based on caller-supplied filtering function 
         if (nodeFilter && (nodeFilter( this) ==  false)) { 
                 return ''; 
        } 
         var c =  false, result =  "{"

//  Add the id attribute unless the attribute filter rejects it. 
         if (!attributeFilter || attributeFilter( "id"this.id)) { 
                result += ' "id:"' +  this.id; 
                c =  true
        } 

//  Add all user-added attributes unless rejected by the attributeFilter. 
         for( var key  in  this.attributes) { 
                 if ((key != 'id') && (!attributeFilter || attributeFilter(key,  this.attributes[key]))) { 
                  if (c) result += ','; 
       if (attributeMapping && attributeMapping[key]) { 
        thisKey = attributeMapping[key]; 
      }  else { 
        thisKey = key; 
      } 
                 result += ' "' + thisKey + '": "' + this.attributes[key] + '"'; 
                 c =  true
         } 
        } 

//  Add child nodes if any 
         var children =  this.childNodes; 
         var clen = children.length; 
         if(clen != 0){ 
                 if (c) result += ','; 
                result += ' "children":['; 
                 for( var i = 0; i < clen; i++){ 
                         if (i > 0) result += ','; 
                        result += children[i].toJsonString(nodeFilter, attributeFilter, attributeMapping); 
                } 
                result += ']'; 
        } 
         return result +  "}"
}; 

/** 
* Returns a string of XML that represents the tree 
* @param {Function} (optional) A function, which when passed the node, returns true or false to include 
* or exclude the node. 
* @param {Function} (optional) A function, which when passed an attribute name, and an attribute value, 
* returns true or false to include or exclude the attribute. 
* @return {String} 
*/
 
Ext.tree.TreePanel.prototype.toXmlString =  function(nodeFilter, attributeFilter, attributeMapping){ 
   return '\u003C?xml version= "1.0"?>\u003Ctree>' + 
     this.getRootNode().toXmlString(nodeFilter, attributeFilter, attributeMapping) + 
    '\u003C/tree>'; 
}; 

/** 
* Returns a string of XML that represents the node 
* @param {Function} (optional) A function, which when passed the node, returns true or false to include 
* or exclude the node. 
* @param {Function} (optional) A function, which when passed an attribute name, and an attribute value, 
* returns true or false to include or exclude the attribute. 
* @return {String} 
*/
 
Ext.tree.TreeNode.prototype.toXmlString =  function(nodeFilter, attributeFilter, attributeMapping){ 
//  Exclude nodes based on caller-supplied filtering function 
         if (nodeFilter && (nodeFilter( this) ==  false)) { 
                 return ''; 
        } 
         var result = '\u003Cnode'; 

//  Add the id attribute unless the attribute filter rejects it. 
         if (!attributeFilter || attributeFilter( "id"this.id)) { 
                result += ' id= "' + this.id + '"'; 
        } 

//  Add all user-added attributes unless rejected by the attributeFilter. 
         for( var key  in  this.attributes) { 
                 if ((key != 'id') && (!attributeFilter || attributeFilter(key,  this.attributes[key]))) { 
                  if (attributeMapping && attributeMapping[key]) { 
        thisKey = attributeMapping[key]; 
      }  else { 
        thisKey = key; 
      } 
      result += ' ' + thisKey + '= "' + this.attributes[key] + '"'; 
         } 
        } 

//  Add child nodes if any 
         var children =  this.childNodes; 
         var clen = children.length; 
         if(clen == 0){ 
                result += '/>'; 
        } else
                result += '>'; 
                 for( var i = 0; i < clen; i++){ 
                        result += children[i].toXmlString(nodeFilter, attributeFilter, attributeMapping); 
                } 
                result += '\u003C/node>'; 
        } 
         return result; 
};
 
应用
   function contextmenu(node, e) { 
    node.select(); 
     if (node.isLeaf()) { 
       var json = node.toJsonString( nullfunction(key, val) { 
         return (key == 'loginfo'); 
      }, { 
        loginfo : 'loginfo' 
      }); 
      alert( "步骤日志:" + json); 
    } 
  }
本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/751928,如需转载请自行联系原作者
相关文章
2. ByteBuffer
2. ByteBuffer
121 0
|
移动开发 Shell
蓝桥杯:2020 国赛 例题:天干地支
蓝桥杯:2020 国赛 例题:天干地支
247 0
|
前端开发
Typora使用技巧1 —— 更改字体、大小、颜色、背景色,设置图片默认左对齐,修改图片保存路径
Typora使用技巧1 —— 更改字体、大小、颜色、背景色,设置图片默认左对齐,修改图片保存路径
2185 3
|
存储 安全 数据挖掘
性能30%↑|阿里云AnalyticDB X AMD EPYC,数据分析步入Next Level
阿里云原生数仓 AnalyticDB for PostgreSQL 与 AMD 新一代硬件深度优化,结合全自研计算引擎及行列混合存储实现性能升级,综合性能提升30%。结合丰富的企业级能力帮助企业构建离在线一体、流批一体综合数据分析平台,采用同一引擎即可满足离线批处理、流式加工,交互式分析三种场景,在开发运维、时效性及成本上具备更高的性价比。
755 0
|
存储 Linux 开发工具
Linux内核开发流程指南 - 2. 开发流程的运作方式【ChatGPT】
Linux内核开发流程指南 - 2. 开发流程的运作方式【ChatGPT】
|
搜索推荐
业务系统架构实践问题之过细的扩展点颗粒度可能带来问题如何解决
业务系统架构实践问题之过细的扩展点颗粒度可能带来问题如何解决
147 0
|
JavaScript Java 测试技术
基于ssm+vue.js+uniapp小程序的疫苗预约系统附带文章和源代码设计说明文档ppt
基于ssm+vue.js+uniapp小程序的疫苗预约系统附带文章和源代码设计说明文档ppt
157 0
|
存储 C++
【C++练级之路】【Lv.15】AVL树(双子旋转,领略绝对平衡之美)
【C++练级之路】【Lv.15】AVL树(双子旋转,领略绝对平衡之美)
137 0
【C++练级之路】【Lv.15】AVL树(双子旋转,领略绝对平衡之美)
|
SQL 数据库 C语言
使用SQL语句实现数据插入、修改和删除操作
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句实现数据插入、修改和删除操作。
|
Unix Docker 容器
Win10 WSL运行docker报错:Cannot connect to the Docker daemon at unix:///var/run/docker.sock.
Win10 WSL运行docker报错:Cannot connect to the Docker daemon at unix:///var/run/docker.sock.
1204 0
Win10 WSL运行docker报错:Cannot connect to the Docker daemon at unix:///var/run/docker.sock.