ztree刷新后 展开刷新前打开的节点
时间: 2023-10-09 10:16:16 浏览: 52
在 zTree 中,可以使用 `beforeReload` 和 `onAsyncSuccess` 两个事件来实现刷新后展开刷新前打开的节点的功能。
具体步骤如下:
1. 在 `beforeReload` 事件中,将当前已经展开的节点的 ID 存储到一个数组中,例如 `expandedNodeIds`。
```
var expandedNodeIds = [];
function beforeReload(treeId, treeNode) {
var nodes = zTree.getNodesByParam("isParent", true);
for (var i = 0, l = nodes.length; i < l; i++) {
if (nodes[i].open) {
expandedNodeIds.push(nodes[i].id);
}
}
}
```
2. 在 `onAsyncSuccess` 事件中,对存储在 `expandedNodeIds` 数组中的节点进行展开操作。
```
function onAsyncSuccess(event, treeId, treeNode, msg) {
for (var i = 0, l = expandedNodeIds.length; i < l; i++) {
var node = zTree.getNodeByParam("id", expandedNodeIds[i]);
if (node) {
zTree.expandNode(node, true, false, false);
}
}
expandedNodeIds = [];
}
```
完整代码如下所示:
```
var expandedNodeIds = [];
function beforeReload(treeId, treeNode) {
var nodes = zTree.getNodesByParam("isParent", true);
for (var i = 0, l = nodes.length; i < l; i++) {
if (nodes[i].open) {
expandedNodeIds.push(nodes[i].id);
}
}
}
function onAsyncSuccess(event, treeId, treeNode, msg) {
for (var i = 0, l = expandedNodeIds.length; i < l; i++) {
var node = zTree.getNodeByParam("id", expandedNodeIds[i]);
if (node) {
zTree.expandNode(node, true, false, false);
}
}
expandedNodeIds = [];
}
```
在 zTree 的配置中,将 `beforeReload` 和 `onAsyncSuccess` 事件分别绑定到对应的属性上即可。
```
var setting = {
// ...
callback: {
beforeReload: beforeReload,
onAsyncSuccess: onAsyncSuccess
}
};
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)