After operating on the tree node, you often need to perform a reload operation to refresh the tree, but many businesses need to select the last selected node by default after the tree is refreshed. In this way, you must first save the information of the selected node the previous time, and then expand it layer by layer through the information of the node after reloading.
After searching for a long time, I finally found a feasible solution, which is to record the node's location information through the node's path, and then expand layer by layer from the root node through the path until the last node.
The finished code is as follows:
First, the method in extjs3.x version:
The code copy is as follows:
//Get selected node
var node = tree.getSelectionModel().getSelectedNode();
if(node == null) { //The overload tree is not selected
tree.getRootNode().reload();
} else { //Reload the tree and select the last selected node by default
var path = node.getPath('id');
tree.getLoader().load(tree.getRootNode(),
function(treeNode) {
tree.expandPath(path, 'id', function(bSucess, oLastNode) {
tree.getSelectionModel().select(oLastNode);
});
}, this);
}
The difference between Extjs3.0 and Extjs4.2 is as follows
The code copy is as follows:
idPath = selNode.getPath("id");
tree.getStore().load({
node: tree.getRootNode(),
callback: function () {
tree.expandPath(idPath, 'id');
}
});
It should be noted that when the json data of the tree returned in the background, the node must contain the id attribute. Originally, I did not have this attribute, but I changed the parameters in the getPath method to another attribute. It turns out that this cannot achieve the effect, and finally, the id attribute was added to json.