Search code examples
jstree

how to Restrict the number of children of parent selected in jstree?


how i can Restrict the number of children selected of parent? please help me

function buildTree(data) {
    var dataJson = JSON.parse(data);
    $("#onflycheckboxes").jstree({
        "json_data": {
            data: dataJson,
        },
        checkbox: {
            real_checkboxes: true,
            checked_parent_open: true,
            three_state: false
        },
        "search": {
            "case_insensitive": true,
            "show_only_matches": true
        },
        "plugins": ["themes", "json_data", "ui", "checkbox", "search"]

    });
}

Solution

  • for check nodes you must use $("#onflycheckboxes").jstree('get_checked')in jstree

     function buildTree(data) {
    var dataJson = JSON.parse(data);
    $("#onflycheckboxes").jstree({
        "json_data": {
            data: dataJson,
        },
        checkbox: {
            real_checkboxes: true,
            checked_parent_open: true,
            three_state: false
        },
        "search": {
            "case_insensitive": true,
            "show_only_matches": true
        },
        "plugins": ["themes", "json_data", "ui", "checkbox", "search"]
    
    }).bind("before.jstree", function (e, data) {
    
            if (data.func === "check_node") {
                if ($("#onflycheckboxes").jstree('get_checked').length >= numberOfCompanies) {
                    e.preventDefault();
    
                    toastMessage();//your message for show to user can't select any more
                    return false;
                }
            }
        });;