0

I'm creating a tree using jstree javascript library https://www.jstree.com/ .I need to create a new node just after current node as a sibling.

  • Elem 1
  • Elem 2
  • Elem 3

My point is when i click on Elem 2 a new node say Elem 2.5 is added in between Elem 2 and Elem 3

so result:--

  • Elem 1
  • Elem 2
  • Elem 2.5
  • Elem 3

My current code: $(".appendTree").jstree('create_node', CurrentNode, html, 'last');

by this new ,nodes are always added at the end.is there any api or workaround to handle this??

Nikolay Ermakov
  • 5,031
  • 2
  • 11
  • 18

1 Answers1

0

You will have to figure out the current index for the selected node in the current folder, then pass the index to create_node method instead of your last. See code below. Check demo - Fiddle Demo.

$('.appendTree')
        .jstree({   
        core : {
            check_callback : true,
            data : coredata
        }
    })
    .on("select_node.jstree", function (e, data) {
        var $parent = $('#'+data.node.id).parent();
                var index = $parent.children().index( $('#'+data.node.id) ) +1;

        if(data.node.parent === '#') {
            $parent = '#';
        }

        $(".appendTree").jstree('create_node', $parent, 'new node', index);
    }); 
Nikolay Ermakov
  • 5,031
  • 2
  • 11
  • 18