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( null, function(key, val) {
         return (key == 'loginfo');
      }, {
        loginfo : 'loginfo'
      });
      alert( "步骤日志:" + json);
    }
  }

你可能感兴趣的:(TO,职场,web前端,ExtJs,休闲,jsonstring)