简体   繁体   English

Jstree - 每次只允许检查所有 Jstree 中的一个节点

[英]Jstree - only allow to check one node each time in all the Jstree

I have a jstree fill with data and i just want to limit the user to only check one node in all the jstree and when check another uncheck the first and check the other one.我有一个 jstree 填充数据,我只想限制用户只检查所有 jstree 中的一个节点,当检查另一个节点时,取消选中第一个并检查另一个。

Checkbox property multiple to false not working.复选框属性多到 false 不起作用。 My checkbox plugin is configured like this:我的复选框插件配置如下:

    'checkbox': {
        'whole_node': false,
        'tie_selection': false,
        'multiple': false,
        'three_state': false,
        'cascade': 'down'
    },

Thank you.谢谢你。

want to limit the user to only check one node in all the jstree and when check another uncheck the first and check the other one.想要限制用户只检查所有 jstree 中的一个节点,当检查另一个时取消选中第一个并检查另一个。

To achieve your above requirement, you can refer to the following code snippet.为了实现您的上述要求,您可以参考以下代码片段。

Hide checkbox(es) of root nodes隐藏根节点的复选框

<style>
    .jstree li.jstree-open > a.jstree-anchor > i.jstree-checkbox,
    .jstree li.jstree-closed > a.jstree-anchor > i.jstree-checkbox {
        display: none;
    }
</style>

Html code html代码

<div id="tree">
    <ul>
        <li id="pnode_1">
            ParentNode 1
            <ul>
                <li id="child1_1">ChildNode 1</li>
                <li id="child1_2">ChildNode 2</li>
            </ul>
        </li>
        <li id="pnode_2">
            ParentNode 2
            <ul>
                <li id="child2_1">ChildNode 1</li>
                <li id="child2_2">ChildNode 2</li>
                <li id="child2_3">ChildNode 3</li>
            </ul>
        </li>
       
    </ul>
</div>

JS code JS代码

<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<script>
    $(function () {
        var Tree = $("#tree").jstree({
            'checkbox': {
                'whole_node': false,
                'tie_selection': false,
                'keep_selected_style': false,
                'cascade': 'down'
            },
            "plugins": ["checkbox"]
        });

        //overwrite default behaviour of check_node function

        Tree.on("check_node.jstree", function (e, data) {
            var selectedNodes = $("#tree").jstree().get_checked();
            console.log(selectedNodes);

            console.log(data.node.id);

            if (selectedNodes.length > 1) {
                $("#tree").jstree().uncheck_node(selectedNodes[0]);
            }
        });
    });
</script>

Test Result测试结果

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM