0
我正嘗試從可編輯的treepanel生成JSON。我能夠生成JSON,但希望JSON只有某些字段。如何從TreePanel生成自定義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
}
]
}
]
}
請幫助。提前致謝。
那麼這並沒有爲我工作。現在它顯示在警報中未定義。 – user2143272
你已經注意到我沒有使用與你相同的函數名稱,對吧? – rixo
我的壞...對不起!我當時請不理會以前的評論。我正在做一些非常愚蠢的事情。有效。非常感謝!是的,我注意到函數名稱不同。 – user2143272