javascript - JsTree checkbox - 检查事件

标签 javascript jquery jstree

我有这个 JsTree

enter image description here

使用此代码:

var Tree = $("#MyTree");

        Tree.jstree({
            "core": {
                "themes": {
                    "responsive": false,
                    "multiple" : false,
                },
                "data": dataTree
            },
            "types": {
                "default": {
                    "icon": "icon-lg"
                },
                "file": {
                    "icon": "icon-lg"
                }
            },
            "ui": {
                "select_limit": 1,
            },
            "plugins": ["wholerow", "types", "checkbox", "ui", "crrm", "sort"],
            "checkbox": {
                "three_state": false,
                "real_checkboxes": false
            }
        });

我需要将选择和检查操作分开,用户必须检查他想要的所有节点,但只选择一行时间。 现在,当我点击该行的任何地方时,它会选择该行并选中该节点,只有当用户点击它时我才需要选中该复选框。

我尝试了很多事件,但唯一有效的是:

Tree.on("changed.jstree", function (e, data) { });

捕捉选择和检查的 Action 。

有什么建议吗?

最佳答案

这个答案是关于 jstree 的第 3 版——这是你应该在 2016 年使用的。不幸的是,你的示例代码似乎使用的是 jstree rel 1,我无法帮助你。

对于第 3 版

首先,解开选中状态和选中状态 (checkbox.tie_selection: false) - 参见 the docs

然后,使用check_node.jstree事件

工作示例

var data1 = [{
      "id": "W",
      "text": "World",
      "state": { "opened": true },
      "children": [{"text": "Asia"}, 
                   {"text": "Africa"}, 
                   {"text": "Europe",
                    "state": { "opened": false },
                    "children": [ "France","Germany","UK" ]
      }]
    }];

$('#Tree').jstree({ 
    core: {
      data: data1, 
      check_callback: false
    }, 
    checkbox: {       
      three_state : false, // to avoid that fact that checking a node also check others
      whole_node : false,  // to avoid checking the box just clicking the node 
      tie_selection : false // for checking without selecting and selecting without checking
    },
    plugins: ['checkbox']
})
.on("check_node.jstree uncheck_node.jstree", function(e, data) {
  alert(data.node.id + ' ' + data.node.text +
        (data.node.state.checked ? ' CHECKED': ' NOT CHECKED'))
})
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" type="text/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="Tree"></div>

关于javascript - JsTree checkbox - 检查事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502382/

相关文章:

jquery - 如何在 HTML 工具提示的标题中附加值

jquery - 在类/jQuery 之间切换

javascript - 如何让 jstree 与 ajax 加载的内容一起正常工作

javascript - 如何让我随机生成的 div 出现在容器 div 中?

javascript - 如何确定浏览器 session 是刚刚开始还是后续点击?

javascript - 使用 onblur 启动对 php mysql DB 的 ajax 查询

javascript - 为什么我无法使用 URL 参数设置选择选项的值?

javascript - Select2 多选奇怪的叠加

javascript - 在jsTree中,如何通过节点id获取节点信息?

javascript - JsTree检查节点