Search code examples
javascriptjqueryjstreejstree-dndjstree-search

Using Checkbox, When all child nodes are deleted, parent node also getting deleted in JStree


If CheckBox Plugin is used and all child nodes were deleted, the parent node is also getting deleted.

$('#jstree').jstree({
"core" : {
    "check_callback" : true,
    "data" : [
        { "text" : "Branch 1", "type" : "branch", "children" : [
            { "text" : "leaf 1.1", "type" : "leaf" },
            { "text" : "leaf 1.2", "type" : "leaf" },
            { "text" : "leaf 1.3", "type" : "leaf" }
           ]
        },
        { "text" : "Branch 2", "type" : "branch", "children" : [
            { "text" : "leaf 2.1", "type" : "leaf" },
            { "text" : "leaf 2.2", "type" : "leaf" },
            { "text" : "leaf 2.3", "type" : "leaf" }
           ]
        }
    ]
},
        "types" : {
            "#" : {
                "valid_children" : ["branch"]
            },
            "branch" : {
                "valid_children" : ["leaf"]
            },
            "leaf" : {
                "valid_children" : []
            }
        },
"plugins" : ["checkbox","types", "dnd", "contextmenu"]});

Here is a jsFiddle demo. Look into it: http://jsfiddle.net/z8L5r9w3/1/


Solution

  • You could use the jsTree checkbox configuration properties 'three_state' and 'cascade' such that the parent is not selected when all child nodes are selected. Using the configuration below you can disable the parent selection and ensure child nodes are automatically selected for a certain parent node selection.

    "checkbox" : {
      "three_state": false,
      "cascade": "down"
    }
    

    You can also check the jsTree documentation here

    Edit: Removing the cascade property ensures that no child or parent nodes are selected on selection of a node.