【问题标题】:How to Generate a Custom JSON from TreePanel如何从 TreePanel 生成自定义 JSON
【发布时间】:2013-05-31 16:31:17
【问题描述】:

我正在尝试从可编辑的树形面板生成 JSON。我能够生成 JSON,但希望 JSON 只有某些字段。

这是我通过遍历生成 JSON 的方法。

function getNodeList(bfsQueue) {
        var node = bfsQueue.pop();
        var nodeQueue = [];

        for (var ii = 0; ii < node.childNodes.length; ii++) {
            bfsQueue.push( node.childNodes[ii] );
            nodeQueue.push( node.childNodes[ii] );
        }
        if (bfsQueue.length === 0) {
            return nodeQueue;
        } else {
            return nodeQueue.concat( getNodeList(bfsQueue) );
        }
    }

这就是我在提交处理程序中调用函数的地方。

var startQueue = [];
                            var nodeList = [];
                            startQueue.push( tree.getRootNode() );
                            nodeList.push( tree.getRootNode() );
                            nodeList = nodeList.concat(getNodeList( startQueue ));
                            console.dir(nodeList);
                            for ( var nn = nodeList.length-1; nn >= 0; nn-- ) {
                                var params = [];
                                for (var pp in nodeList[nn].data) {
                                    if (pp === "children" || pp === "loader") {continue;}
                                    params.push('"' + pp + '":' + JSON.stringify(nodeList[nn].data[pp]) + '');
                                }
                                if ( nodeList[nn].childNodes.length > 0) {
                                    var childList = [];
                                    for (var ii = 0; ii < nodeList[nn].childNodes.length; ii++) {
                                        childList.push( nodeList[nn].childNodes[ii].json );
                                    }
                                    params.push('"children": [' + childList.join(',') + ']');
                                }
                                nodeList[nn].json = "{" + params.join(",") + "}";
                            }
                            alert("My Root :"+nodeList[0].json);

生成的 JSON 是这样的。

{
"text": "Src",
"id": "src",
"expandable": true,
"expanded": true,
"allowDrag": false,
"parentId": null,
"root": true,
"leaf": "",
"depth": 0,
"index": 0,
"checked": null,
"cls": null,
"iconCls": null,
"isLast": true,
"isFirst": true,
"allowDrop": true,
"loaded": true,
"loading": false,
"href": null,
"hrefTarget": null,
"qtip": null,
"qtitle": null,
"children": [
    {
        "text": "United Kingdom",
        "id": "United Kingdom",
        "parentId": "src",
        "root": "",
        "leaf": "",
        "depth": 1,
        "index": 0,
        "expanded": false,
        "expandable": true,
        "checked": null,
        "cls": "",
        "iconCls": "",
        "isLast": true,
        "isFirst": true,
        "allowDrop": true,
        "allowDrag": true,
        "loaded": true,
        "loading": false,
        "href": "",
        "hrefTarget": "",
        "qtip": "",
        "qtitle": "",
        "children": [
            {
                "text": "London",
                "id": "London",
                "parentId": "United Kingdom",
                "root": "",
                "leaf": "",
                "depth": 2,
                "index": 0,
                "expanded": false,
                "expandable": true,
                "checked": null,
                "cls": "",
                "iconCls": "",
                "isLast": true,
                "isFirst": true,
                "allowDrop": true,
                "allowDrag": true,
                "loaded": false,
                "loading": false,
                "href": "",
                "hrefTarget": "",
                "qtip": "",
                "qtitle": ""
            }
        ]
    }
]

}

而且我需要它采用这种格式。只是几个字段,不是全部。

{
"text": "Src",
"id": "src",
"parentId": null,
"root": true,
"leaf": "",
"depth": 0,
"children": [
    {
        "text": "United Kingdom",
        "id": "United Kingdom",
        "parentId": "src",
        "root": "",
        "leaf": "",
        "depth": 1,
        "children": [
            {
                "text": "London",
                "id": "London",
                "parentId": "United Kingdom",
                "root": "",
                "leaf": "",
                "depth": 2
            }
        ]
    }
]

}

请帮忙。提前致谢。

【问题讨论】:

    标签: extjs getjson treepanel


    【解决方案1】:

    只需在结果中选择您想要的字段。

    例子:

    function getNodeData(node, fields) {
        var data = {};
    
        // loop through desired fields
        Ext.each(fields, function(fieldName) {
            data[fieldName] = node.get(fieldName);
        });
    
        if (node.hasChildNodes()) {
            var children = data.children = [];
            node.eachChild(function(child) {
                children.push(getNodeData(child, fields));
            });
        }
    
        return data;
    }
    

    用法:

    var fields = ['text', 'id', 'parentId', 'root', 'leaf', 'depth'],
        nodeList = getNodeData(tree.getRootNode(), fields);
    

    【讨论】:

    • 嗯,这对我不起作用。现在它在警报中显示未定义。
    • 你注意到我没有用和你一样的函数名吧?
    • 我的错...对不起!!我是请忽略之前的评论。我正在做一些非常愚蠢的事情。有效。非常感谢队友!是的,我确实注意到函数名称不同。
    猜你喜欢
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2017-05-15
    • 2019-09-13
    相关资源
    最近更新 更多