如何以编程方式选择特定节点?
我有一个jstree。我想选择绑定到 id 为 158 的位置的对象的节点。这可行,但看起来很愚蠢。这样做更惯用的方法是什么?
var $tree = $('.jstree', myContext),
node = $tree.find('li').filter(function() {
return ( $(this).data().location || {}).id === 158;
});
$tree.jstree('select_node', n)
I have a jstree. I want to select the node which is bound to the object which has a location with id of 158. This works but seems stupid. What's the more idiomatic way of doing this?
var $tree = $('.jstree', myContext),
node = $tree.find('li').filter(function() {
return ( $(this).data().location || {}).id === 158;
});
$tree.jstree('select_node', n)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
只是想在这里插话,因为没有一个答案对我有用。最终所做的工作非常简单:
$('#someTree').jstree('select_node', 'someNodeId');
请注意,我没有将
someNodeId
初始化为一个 jQuery 对象。这只是一个普通的字符串。我在加载树后立即执行此操作,没有将其放入“就绪”绑定事件中,因为这似乎没有必要。
希望它能让一些人摆脱几个令人沮丧的小时。 。 。
加载后挂接到树上:
Just wanted to chime in here as none of the answers worked for me. What finally DID work was very simple:
$('#someTree').jstree('select_node', 'someNodeId');
Note that I didn't initialize
someNodeId
as a jQuery object. It's just a plain string.I did this right after a tree was loaded without putting it into a "ready" bind event as it seems to not be necessary.
Hope it saves some one from a few frustrating hours. . .
To hook into the tree after it has been loaded:
基于 jsTree groups 你可以尝试
如果数据看起来像
并且 jsTree 设置
是这样的是在之后吗?
Based on jsTree groups you can try
if the data looks like
and jsTree set up
Is that what you are after?
做到了:
我用以下代码
I did it with:
this code:
我能够模拟点击 jstree 节点作为选择节点的替代方法。
使用的代码如下:
I was able to simulate a click on a jstree node as an alternative way to select a node.
The following code is what was used :
如果您使用 HTML 而不是 JSON 数据填充树,并且想知道如何设置
node_id
,则只需设置元素的 id 属性!
然后
将选择
Node 3 - Level 2
节点。对于那些遇到 javascript 错误的人,请记住使用
jQuery
的完整版本,而不是精简版本!对于所有反对者,以下是证明其有效的演示:
https://jsfiddle.net/davidliang2008/75v3fLbs/7/
If you're populating the tree using HTML instead of JSON data and wondering how to set the
node_id
, you just need to set the id attribute of the<li>
element!Then
will select the
Node 3 - Level 2
node.For those getting javascript errors, remember to use Full version of
jQuery
, not the slim version!For all down voters, here is the demo to prove it's working:
https://jsfiddle.net/davidliang2008/75v3fLbs/7/
我使用jstree 3.0.8。不要使用“状态”
并且服务器提供 json,所选节点有
i use jstree 3.0.8. don't use 'state'
and server offer the json, the selected node has
这个解决方案对我有用
,甚至在 php 循环中(动态):
希望这对你有用。
This solution Works for me
and even in a php loop (dynamically) :
Hope this works for you.
我认为你应该在 jstree 初始化后编写代码来选择节点,因此使用此代码
希望它能工作:)
i think u should write code to select node after jstree initialized, therefore use this code
hope its work :)
触发点击第一个锚点
或按节点 ID 158
trigger click on first anchor
or by node id 158