获取Ext Tree/TreeStore加载的原始JSON数据

  本源代码将Ext的Tree对象或者TreeStore加载的数据源转换为原始的json对象。如果对tree对象节点增加删除后,需要保存结果到服务器的json文件中,而不是数据库,可以将递归遍历TreeStore获取原始数据并重建对应的JSON对象。

获取Ext Tree/TreeStore加载的原始JSON数据

  获取Ext Tree/TreeStore加载的原始JSON数据源代码如下,测试通过版本:ext-4.1.1a(ext4-没有Ext.data.NodeInterface接口,代码不兼容Ext4-一下版本的

        function buildJSONStruct(childNodes) {//递归遍历NodeInterface生成json对象
            var siblings = [], json;
            for (var i = 0; i < childNodes.length; i++) {
                json = childNodes[i].raw;
                siblings[siblings.length] = json;
                if (childNodes[i].childNodes && childNodes[i].childNodes.length > 0) json.children = buildJSONStruct(childNodes[i].childNodes);
            }
            return siblings;
        }
        function SerializeTreeStoreToJsonData(tree) {//参数可以为tree对象或者treestore对象
            var rootNode = tree.getRootNode(), json = rootNode.raw;
            if (rootNode.childNodes && rootNode.childNodes.length > 0) json.children = buildJSONStruct(rootNode.childNodes);
            var s = Ext.encode(json); //s为对应JSON格式字符串,要保存到服务器用Ext.ajax来发送这些内容
            console.log(json);
        }

 

加支付宝好友偷能量挖...


原创文章,转载请注明出处:获取Ext Tree/TreeStore加载的原始JSON数据

评论(0)Web开发网
阅读(765)喜欢(0)extjs开发技巧