Parent node check in jstree

2.4k views Asked by At

I have a jstree as shown below:

$('#FolderTree').jstree({
            'core': {
                'data': [
                            {
                                'text': 'Claim key',
                                'state': {
                                    'opened': false,
                                    'selected': false
                                },
                                'children': claimKeys
                           },
                            {
                                'text': 'Client',
                                'state': {
                                    'opened': false,
                                    'selected': false
                                },
                                'children': clients
                            }
                         ]
                  },

            "plugins": ["checkbox"]
        });

For click event of checkbox I'm using the below jquery :

$('#FolderTree').on("select_node.jstree", function (e, data) {
            var checkedValue = data.node.text;
  });

But I want to first determine if the checkbox I've clicked is parent node or child node..How can I do that??

1

There are 1 answers

0
vakata On

You can use this code:

$('#FolderTree').on("select_node.jstree", function (e, data) {
    var checkedValue = data.node.text;
    var isParent = data.instance.is_parent(); 
    // you can also use is_leaf() to check the opposite
});

is_parent will tell you if the node has any children, is_leaf will tell you if it is a leaf node (if it has no children) - use one or the other.

If you need to check if a node is a root node you can use:
var isRoot = (data.node.parents.length === 1)