Javascript – jsTree checkbox check_node is not working

javascriptjqueryjstree

jstree v1.0

I have just made modification on checkbox example: http://www.jstree.com/documentation/checkbox

My goal:
When checking any of the nodes I want all parent node to be checked (default behavior is not OK, because that is something partially selected which is not good for me)

based on tips and documentation i have extended the original example but I have found that the check_node method is not working. I have not noticed any effect.

Any tips or advice is welcomed.

<div id="demo1" class="demo">
    <ul>
        <li id="phtml_1">
            <a href="#">Root node 1</a>
            <ul>
                <li id="phtml_2" >
                    <a href="#">Child node 1</a>
                </li>
                <li id="phtml_3">
                    <a href="#">A Child node 2</a>
                </li>
            </ul>
        </li>
        <li id="phtml_4">
            <a href="#">Root node 2</a>
        </li>
        <li id="phtml_5">
            <a href="#">Root node 1</a>
            <ul>
                <li id="phtml_51">
                    <a href="#">Child node 1</a>
                </li>
                <li id="phtml_52">
                    <a href="#">A Child node 2</a>
                </li>
            </ul>
        </li>
        <li id="phtml_6">
            <a href="#">Root node 1</a>
            <ul>
                <li id="phtml_61">
                    <a href="#">Child node 1</a>
                </li>
                <li id="phtml_62">
                    <a href="#">A Child node 2</a>
                </li>
            </ul>
        </li>
    </ul>
</div>
<script type="text/javascript" class="source">
$(function () {
    var x = $("#demo1");
    $(x).jstree({
        "checkbox" :{
            real_checkboxes:false,
            override_ui:true,
            two_state:false,    
        },
        "plugins" : [ "themes", "html_data", "checkbox", "ui" ]
    }).bind('check_node.jstree', function (e, data) { 
        console.log('check_node.jstree ----------------------------------------------');
        console.log(e);
        var all_selected = $(x).jstree('get_checked')
        console.log('all_selected='+all_selected);
        for(var i=0;i<all_selected.length;i++){
            var paths = $(x).jstree('get_path', all_selected[i], true);
            console.log('  paths='+paths);
            for(var j=0; j< paths.length;j++){
                console.log('    checking node (not working)='+paths[j]);
                $(x).jstree('check_node',paths[j]);
            }
        }
    });
    console.log('programaticcaly checking last parent node (not working)')
    //$(x).jstree('check_node',$('li#phtml_6'));
    $.jstree._reference("#demo1").check_node('li#phtml_6');
});
</script>

Best Answer

The following code was working for me...

var contact_list = $('#compose_contact_list');

    contact_list.jstree({
        "checkbox" : {
            "keep_selected_style" : false,
            "tie_selection" : false
        },
        "plugins" : [ "checkbox" ]
    });

    contact_list.on("check_node.jstree", function(event, data){
        alert(data.node.text);
    });