Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsTree checkbox - check event

I have this JsTree

enter image description here

with this code:

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

I need to separate the selection and the check action, the user must check all node he wants but select only one row for time. for now when I click everywhere on the row it select that row and check that node, i need to check the checkbox only if the user click on it.

I try so much event but the only that work is:

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

that catch both the action of selection and check.

Any suggestions?

like image 492
Lorenzo Grossi Avatar asked Feb 19 '16 09:02

Lorenzo Grossi


1 Answers

This answer is about the release 3 of jstree - that is what you should use in year 2016. Unfortunaltely your sample code, seems using jstree rel 1, and I can't help you about that.

For release 3

First of all, untie the selected and the checked states (checkbox.tie_selection: false) - see the docs

Then, use the check_node.jstree event

Working example

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>
like image 198
edc65 Avatar answered Oct 01 '22 08:10

edc65