javascript jstree 禁用复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8487305/
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
jstree disable checkbox
提问by Pavan Josyula
I am currently working on some POC using JS Tree plugin and related check box plugin. For certain nodes I need to check the check box by default and disable any further selection.I found the function to hide the check box
我目前正在使用 JS Tree 插件和相关复选框插件处理一些 POC。对于某些节点,我需要默认选中复选框并禁用任何进一步的选择。我找到了隐藏复选框的功能
.bind("load_node.jstree", function (e, data) {
$(this).find('li[rel!=file]').find('.jstree-checkbox:first').hide();
});
instead of hiding the check box completely I want to find a way to disable check box for certain nodes
而不是完全隐藏复选框,我想找到一种方法来禁用某些节点的复选框
回答by mcabral
You will need to define a "disabled" type (using the types plugin) and then assign that type to the desired node.
您需要定义一个“禁用”类型(使用类型插件),然后将该类型分配给所需的节点。
Take for instance this "disabled" type definition:
以这个“禁用”类型定义为例:
"types" : {
"types": {
"disabled" : {
"check_node" : false,
"uncheck_node" : false
}
}
}
and the type assigment:
和类型分配:
$.jstree._reference('#tree').set_type("disabled", "#node5");
More info on the types plugin can be found hereand you can also check this google group with more info on disabling checkboxes
可以在此处找到有关类型插件的更多信息,您还可以查看此 google 群组,了解有关禁用复选框的更多信息
Hope it helps!
希望能帮助到你!
回答by Sergio
Thank you to mcabral and Tomasz for their answer. It helped me to achieve the right result. However, I needed to add some extra lines in order to get it working properly. Here is what I did:
感谢 mcabral 和 Tomasz 的回答。它帮助我取得了正确的结果。但是,我需要添加一些额外的行才能使其正常工作。这是我所做的:
You need to add two attributes to the <li>
tag wich are the rel='disable'
to indicate jstree that this will be the new type for the checkbox, instead of default and the class='jstree-checked'
attribute which will pre-check the checkboxes when loading the tree.
您需要在<li>
标签中添加两个属性,分别是rel='disable'
指示 jstree 这将是复选框的新类型,而不是默认值,以及class='jstree-checked'
在加载树时预检查复选框的属性。
$rel = ( 'if the checkbox need to be pre-checked' )? 'rel="disabled" class="jstree-checked"' : '';
echo '<li id="checkbox_id" '. $rel .'>';
Then based on the previous answer you need to define the 'disable' type that was used in the rel attribute as follows:
然后根据之前的答案,您需要定义 rel 属性中使用的“禁用”类型,如下所示:
.jstree({
"types" :
{
"types" : {
"disabled" : {
"check_node" : false,
"uncheck_node" : false
}
}
},
"plugins" : ["themes","html_data","ui","crrm","types", "checkbox"],
"checkbox" : { "two_state" : true },
})
回答by zanderwar
If your item is static; for example.... always there like in my scenario.
如果您的项目是静态的;例如......总是像在我的场景中一样。
I just simply hid the checkbox on that item with CSS. As I do server side checking, any malicious attempt at altering it will be discarded anyway.
我只是简单地用 CSS 隐藏了该项目上的复选框。当我进行服务器端检查时,任何恶意更改它的尝试都将被丢弃。
#my_item_id .jstree-checkbox {
display: none;
}