jsTree:刷新后如何选择节点

发布于 2024-11-15 15:12:18 字数 167 浏览 4 评论 0原文

我有一个通过 ajax 调用从服务器填充的 jQuery jsTree。当我添加一个新节点时,我会进行 ajax 调用,然后使用 tree.jstree("refresh") 调用刷新树。刷新后我想选择刚刚添加的节点。不幸的是,似乎没有可以传递给此命令的回调。有什么干净的方法可以做到这一点吗?

I have a jQuery jsTree populated from the server via an ajax call. When I add a new node I make an ajax call then make a call to refresh the tree with tree.jstree("refresh"). After the refresh I want to select the node I just added. Unfortunately there doesn't seem to be a callback that can be passed to this command. Is there any clean way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁我热情 2024-11-22 15:12:18

哦,自从这篇文章发布以来已经很久了......但仍然无法在互联网上找到答案。
因此,经过几个小时...不不不,不是这个,想出了一个解决方案

var jsTreeId = '#jstree'; // or whatever name the jstree has
var jsTreeSelectedItemId = 5; // just an example
var selectedNode = $('#node_'+jsTreeSelectedItemId);
var parentNode = $.jstree._reference(jsTreeId)._get_parent(selectedNode);

// 现在假设您从服务器端添加一个新节点,您可以通过 ajax 调用获取所创建节点的新 id,接下来您要刷新树以显示它,并选择它

var newSelectId = 9; // or from ajax call
// call the refresh function, which is asnyc
$.jstree._reference(jsTreeId).refresh(parentNode); 
 // set the magic "to_select" variable with an array of node ids to be selected
// note: this must be set after refresh is called, otherwise won't work
$.jstree._reference(jsTreeId).data.ui.to_select = ['#node_'+newSelectId];

oh, such a long time since this post ... and still couldn't find an answer on internet.
So after a few hours of ... no no no, not this, came up with a solutin

var jsTreeId = '#jstree'; // or whatever name the jstree has
var jsTreeSelectedItemId = 5; // just an example
var selectedNode = $('#node_'+jsTreeSelectedItemId);
var parentNode = $.jstree._reference(jsTreeId)._get_parent(selectedNode);

// now lets say that you add a new node from server side, you get the new id of the created node by an ajax call, and next you want to refresh the tree in order to display it, and also select it

var newSelectId = 9; // or from ajax call
// call the refresh function, which is asnyc
$.jstree._reference(jsTreeId).refresh(parentNode); 
 // set the magic "to_select" variable with an array of node ids to be selected
// note: this must be set after refresh is called, otherwise won't work
$.jstree._reference(jsTreeId).data.ui.to_select = ['#node_'+newSelectId];
源来凯始玺欢你 2024-11-22 15:12:18
$('#tree').jstree("select_node", '#1', true);
//other node are deselected if pass last argument as true.

$('#tree').jstree("select_node", '#1', false);
//other node are selected and new one also selected.
$('#tree').jstree("select_node", '#1', true);
//other node are deselected if pass last argument as true.

$('#tree').jstree("select_node", '#1', false);
//other node are selected and new one also selected.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文