使用jstree是否可以阻止单个特定节点的崩溃?
我有一个树结构,可以随意扩展/折叠。。。但我有一些节点应该始终是打开的,不允许用户折叠它们。
有没有办法阻止崩溃(可能通过<li>
上的data-jstree
属性(。。。如果可能的话,还有一种方法可以去除物品上的三角形?
我已经尝试挂接close_node.jstree
事件,但使用return false
或e.preventDefault()
都无法阻止它的发生。
在以下示例中,我希望"项目2-必须保持打开"始终处于打开状态,并且不允许用户隐藏"始终可见"项目。。。
$(function() {
$("#treeRoot").jstree({
core: {
themes: {
variant: "small"
}
}
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<div id="treeRoot">
<ul>
<li data-jstree='{"opened":true}'>Root
<ul>
<li>Item 1</li>
<li data-jstree='{"opened":true}'>Item 2 - Must keep open
<ul>
<li>Always Visible</li>
</ul>
</li>
<li>Item 3</li>
</ul>
</li>
</ul>
</div>
除了重写jstree代码的close_node
函数之外,我找不到任何防止关闭节点的选项。
这里有一个例子:
$(function() {
$.jstree.core.prototype.old_close_node = $.jstree.core.prototype.close_node;
$.jstree.core.prototype.close_node = function(obj, animation) {
node_obj = this.get_node(obj);
// in case the flag exists - don't continue to with the original function.
if (node_obj.state.prevent_close) {
return;
}
this.old_close_node(obj, animation);
}
$("#treeRoot").on('loaded.jstree', function() {
$('#treeRoot li').each((index, el) => {
if ($(el).data('jstree') && $(el).data('jstree').prevent_close) {
$(el).addClass('prevent-close');
}
})
}).jstree({
core: {
themes: {
variant: "small"
}
}
})
});
.jstree-default-small .jstree-open.prevent-close>.jstree-ocl {
background-position: -71px -7px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<div id="treeRoot">
<ul>
<li data-jstree='{"opened":true}'>Root
<ul>
<li>Item 1</li>
<!-- v-- this is the flag to prevent the close -->
<li data-jstree='{"opened":true, "prevent_close": true}'>Item 2 - Must keep open
<ul>
<li>Always Visible</li>
</ul>
</li>
<li>Item 3</li>
</ul>
</li>
</ul>
</div>
我没有时间处理三角形,如果你有问题,请告诉我,我也会努力找到解决方案。