我有一个jstree。 我想选择绑定到id为158的位置的对象的节点。这可行,但似乎很愚蠢。 这样做的惯用方法是什么?
var $tree = $('.jstree', myContext),
node = $tree.find('li').filter(function() {
return ( $(this).data().location || {}).id === 158;
});
$tree.jstree('select_node', n)
我有一个jstree。 我想选择绑定到id为158的位置的对象的节点。这可行,但似乎很愚蠢。 这样做的惯用方法是什么?
var $tree = $('.jstree', myContext),
node = $tree.find('li').filter(function() {
return ( $(this).data().location || {}).id === 158;
});
$tree.jstree('select_node', n)
只是想在这里说话,因为没有一个答案对我有用。 最后DID的工作非常简单:
$('#someTree').jstree('select_node', 'someNodeId');
请注意,我没有将someNodeId
初始化为jQuery对象。 它只是一个简单的字符串。
我在加载树之后立即执行此操作而未将其置于“准备好”绑定事件中,因为它似乎没有必要。
希望它可以节省一些令人沮丧的时间。 。 。
在加载后挂钩到树中:
.on('loaded.jstree', function() {
// Do something here...
});
基于jsTree组,您可以尝试
jQuery("#getStartedTree").jstree("select_node", "#test2");
如果数据看起来像
The JSON in the TextFile.txt - borrowed from your simple example
[
{
"data" : "A node",
"children" : [ "Child 1", "Child 2" ]
},
{
"attr" : { "id" : "test1" },
"data" : {
"title" : "Long format demo",
"attr" : { "id" : "test2", "href" : "#" }
}
}
]
和jsTree设置
My tree container is <div id="getStartedTree">
My jsTree code
$("#getStartedTree").jstree({
"themes": {
"theme": "default",
"url": "../App_Css/Themes/Default/style.css",
"dots": true,
"icons": true
},
"json_data": {
"ajax": {
"url": "../SiteMaps/TextFile.txt",
"dataType": "json",
"data": function(n) {
return { id: n.attr ? n.attr("id") : 0 };
}
}
},
"plugins": ["themes", "json_data", "ui"]
});
这就是你追求的吗?
我能够模拟jstree节点上的单击作为选择节点的替代方法。 以下代码是使用的:
$(treeIdHandle + " li[id=" + nodeId + "] a").click();
我做到了:
$('.jstree').jstree(true).select_node('element id');
这段代码:
jQuery.each(produto.categorias, function(i, categoria) {
$('#lista-categorias').jstree(true).select_node(categoria.dadoCategoria.id);
});
我使用jstree 3.0.8。 不要使用'国家'
'plugins' : ['dnd','sort','types','contextmenu','wholerow','ui']
和服务器提供json,所选节点具有
"state":{"selected":true,"opened":true}
如果您使用HTML而不是JSON数据填充树并想知道如何设置node_id
, node_id
需要设置<li>
元素的id属性!
<div class="tree-menu">
<ul class="menu">
<li id="node_1">
Node 1 - Level 1
<ul class="menu">
<li id="node_3">
Node 3 - Level 2
</li>
</ul>
</li>
<li id="node_2">
Node 2 - Level 1
</li>
</ul>
</div>
然后
$('.tree-menu')
.jstree(true)
.select_node("node_3");
将选择Node 3 - Level 2
节点。
对于那些得到javascript错误的人 ,记得使用完整版的jQuery
,而不是瘦身版!
对于所有失败的选民 ,这里是证明它正在运作的演示: https : //jsfiddle.net/davidliang2008/75v3fLbs/7/
这个解决方案适合我
// after the tree is loaded
$(".jstree").on("loaded.jstree", function(){
// don't use "#" for ID
$('.jstree').jstree(true).select_node('ElementId');
});
甚至在php循环中(动态):
$(".jstree").on("loaded.jstree", function(){
<?php foreach($tree as $node): ?>
$('.jstree').jstree(true).select_node('<?=$node?>');
<?php endforeach;?>
});
希望这对你有用。