jQuery 如何用新数据重绘jstree树?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22428921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redraw jstree tree with new data?
提问by
So, my question. I initialized my tree with some data:
所以,我的问题。我用一些数据初始化了我的树:
$('#tree').jstree({
'core' : {
'data' : [
'Simple root node',
{
'id' : 'node_2',
'text' : 'Root node with options',
'state' : { 'opened' : true, 'selected' : true },
'children' : [ { 'text' : 'Child 1' }, 'Child 2']
}
]
});
But after some actions, I want to redraw tree with new data. I try to use refreshand redrawmethods from API, but it unsuccessfully.
但是经过一些操作后,我想用新数据重绘树。我尝试使用API 中的刷新和重绘方法,但没有成功。
Can you give me advice, how to refresh tree (without destroy -> create new instance (it works, but it will affect the performance))?
你能给我建议,如何刷新树(不销毁 - > 创建新实例(它可以工作,但会影响性能))?
回答by faxiubite
At version 3 you can reload the tree :
在版本 3 中,您可以重新加载树:
$('#treeId').jstree(true).settings.core.data = newData;
$('#treeId').jstree(true).refresh();
回答by user3926752
refresh()
will refresh the tree to it's initial state, which means that newly created nodes are deleted
refresh()
将树刷新到它的初始状态,这意味着新创建的节点被删除
What you want is redraw(true)
你想要的是 redraw(true)
回答by Neil Cresswell
Try including the check_callback parameter in your core settings, (including the initial load) and ensure it is set to true. Then you should be able to see any changes made. Without this, I've found that I'm not seeing the replacement data, just the original data.
尝试在核心设置中包含 check_callback 参数(包括初始加载)并确保将其设置为 true。然后您应该能够看到所做的任何更改。没有这个,我发现我没有看到替换数据,只有原始数据。
$('#tree').jstree({
'core' : {
'check_callback' : true,
'data' : [
'Simple root node',
{
'id' : 'node_2',
'text' : 'Root node with options',
'state' : { 'opened' : true, 'selected' : true },
'children' : [ { 'text' : 'Child 1' }, 'Child 2']
}
]
});
That handled the refresh part for me, but depending on what you're doing, you could also call $('#tree').jstree('refresh');
.
这为我处理了刷新部分,但根据您在做什么,您也可以调用$('#tree').jstree('refresh');
.