在JSTree中禁用多选不起作用

Disable Multiple Selection in JSTree is not working

本文关键字:不起作用 JSTree      更新时间:2023-09-26

我在应用程序中使用JSTree,代码如下。

this.CreateTreeView = function () {
    $('#jstree_demo_div').jstree({
        'core': {
            'multiple': false,
            'data': [
               { "id": "ajson1", "parent": "#", "text": "Simple root node" },
               { "id": "ajson2", "parent": "#", "text": "Root node 2" },
               { "id": "ajson3", "parent": "ajson2", "text": "Child 1" },
               { "id": "ajson4", "parent": "ajson2", "text": "Child 2" },
            ]
        }
    });
}

如我的代码所示,我正在尝试禁用多选。

现在,当我使用以下代码来选择节点时。

$("#jstree_demo_div").jstree().select_node("ajson3");
$("#jstree_demo_div").jstree().select_node("ajson4");

它仍然选择两个节点。因此,它变得像是从Javascript中进行多选。

我提出这个问题只是为了确认JSTree的工作是否正确

我知道在使用deselect_all函数选择任何节点之前,我可以取消选择所有节点。

但根据我的说法,如果多选设置为false,那么从javascript中选择节点也应该只选择一个节点。

如果我错了,请纠正我。

select_node将选择一个节点,而不考虑multiple的设置。

该设置仅限制用户交互,select_node是一种较低级别的方法,不会受到限制,因此您(开发人员(可以通过编程方式无限制地修改选择。

如果您想使用由用户交互触发的相同功能(因此受multiple限制(,请使用activate_node

只需使用此配置

this.CreateTreeView = function () {
    **"plugins" : [
                "checkbox",  
            ],**  
    $('#jstree_demo_div').jstree({
        'core': {
            **'multiple': false,**
            'data': [
               { "id": "ajson1", "parent": "#", "text": "Simple root node" },
               { "id": "ajson2", "parent": "#", "text": "Root node 2" },
               { "id": "ajson3", "parent": "ajson2", "text": "Child 1" },
               { "id": "ajson4", "parent": "ajson2", "text": "Child 2" },
            ]
        },
        **'checkbox' : {            
            'deselect_all': true,
             'three_state' : false, 
        }**
    }); }
'checkbox' : {            
 'deselect_all': true,
 'three_state' : false, 
}

工作良好!

要禁用JStree中的复选框多选,此代码也可以完美工作:

   var tmp=null;   /// to prevent recursion
              treeobj.on("check_node.jstree uncheck_node.jstree", function(e, data)                 {
                    if(tmp!=data.node.id){      
                        tmp=data.node.id;
                        treeobj.jstree("uncheck_all", null);
                        treeobj.jstree("check_node",data.node.id);
                    }
                })