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);
		}
	}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值