在检查事件中获取jstree中的所有选定节点?

use*_*898 15 html javascript jstree

我正在使用get_bottom_selected来获取JSTree中的所有已检查/选定节点.当我在我的表单中设置一个调用以下方法的按钮时,它可以工作.当我尝试从复选框单击事件调用相同的函数时,它找不到任何选定的节点,即使有一些.

function testit() {
    var data = $('#my_tree').jstree(true).get_bottom_selected(true);
    for(var count = 0; count < data.length; count++){
        // Do Stuff 
    }
}
Run Code Online (Sandbox Code Playgroud)

当以下事件触发时,我想调用该函数并获取所有选定的子节点,但它不起作用.是否有特定于此事件的内容与按钮单击事件调用不同?

.on("check_node.jstree uncheck_node.jstree", function(e, data) {
            testit(); // first line of this function does not get any selected data, even if several are selected. When called from a button click event in my form it does work. 
            });
Run Code Online (Sandbox Code Playgroud)

这是我目前如何设置jstree的方法.

$('#my_tree')
.on("changed.jstree", function (e, data) {
    // Do Stuff
})
.jstree({
checkbox: {
    "keep_selected_style": false,
    "visible" : true,
    "three_state": true,
    "whole_node" : true,
},
plugins: ['checkbox'],
    'core' : {
    'multiple' : true,
    'data' : {
    "url" : "/static/content_data.json",
    "dataType" : "json" 
    }
}
})
.on("check_node.jstree uncheck_node.jstree", function(e, data) {
    testit();
});
Run Code Online (Sandbox Code Playgroud)

Nar*_*dra 7

由于严格模式,如果您尝试使用get_bottom_checked,则会遇到异常

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them. at Function.invokeGetter (<anonymous>:2:14)

data.selected如果您只想要所选节点的ID,则可以使用check或uncheck事件处理程序,但如果需要更多节点,则可以使用'data.instance._model.data'.正如您在我的示例中所看到的,我正在尝试警告是否只选择了一个项目并且状态是打开的.在代码示例中,如果打开`Europe1并选中复选框,则可以看到警报.

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

function testit(data) {
  alert(data.length + ' and ids are ' +data );
  for (var count = 0; count < data.length; count++) {

  }
  
}
$('#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']
})
$('#Tree').on("check_node.jstree uncheck_node.jstree", function(e, data) {
 if (data.selected.length === 1) { alert(data.instance._model.data[data.selected].state['opened']); }
  testit(data.selected);
});
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)