Note that there are some explanatory texts on larger screens.

plurals
  1. POGet JSON Data from ExtJS TreePanel
    text
    copied!<p>Lets say I have a Ext.tree.TreePanel object, and it has data loaded from an external file, ex:</p> <pre><code>var tree = new Ext.tree.TreePanel({ ... loader: new Ext.tree.TreeLoader({ dataUrl:'./some_file.json' }), ... }); </code></pre> <p>This file is an array of objects that define the tree. </p> <p>Lets say the user adds new nodes to the tree and moves some nodes around. Is there away to get the JSON data from the tree so that it could be used for the next time the user loads the tree?</p> <p><strong>EDIT (code solution)</strong>:</p> <p>Here is a solution based on ideas from Juan's response. I'm putting this up in case anyone finds this thread in the future and is looking for some code.</p> <pre><code>function getNodeList(bfsQueue) { var node = bfsQueue.pop(); var nodeQueue = []; for (var ii = 0; ii &lt; 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 &gt;= 0; nn-- ) { var params = []; for (var pp in nodeList[nn].attributes) { if (pp === "children" || pp === "loader") {continue;} params.push('"' + pp + '":' + JSON.stringify(nodeList[nn].attributes[pp]) + ''); } if ( nodeList[nn].childNodes.length &gt; 0) { var childList = []; for (var ii = 0; ii &lt; nodeList[nn].childNodes.length; ii++) { childList.push( nodeList[nn].childNodes[ii].json ); } params.push('"children": [' + childList.join(',') + ']'); } nodeList[nn].json = "{" + params.join(",") + "}"; } console.log(nodeList[0].json); // root node </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload