【问题标题】:Disable Multiple Selection in JSTree is not working在 JSTree 中禁用多项选择不起作用
【发布时间】:2015-09-03 11:59:39
【问题描述】:

我在我的应用程序中使用 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 中选择节点也应该只选择一个节点。

如果我错了,请纠正我。

【问题讨论】:

    标签: javascript jquery jstree


    【解决方案1】:

    select_node 将选择一个节点,而不管multiple 设置如何。

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

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

    【讨论】:

    • 您好 vakata,感谢您的支持。你的库 JSTree 真的很棒。
    【解决方案2】:

    只使用这个配置

    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, 
            }**
    
        }); }
    

    【讨论】:

      【解决方案3】:
      'checkbox' : {            
       'deselect_all': true,
       'three_state' : false, 
      }
      

      工作正常!

      【讨论】:

        【解决方案4】:

        要在 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);
                            }
                        })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-26
          • 1970-01-01
          • 2012-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多