betseyb
betseyb

Reputation: 1332

Unable to dynamically add/delete elements to jstree

I've searched the site, and as per various older answers, I made sure that 'check-callback' was set to 'true'. I also tried using a function in check-callback. I found a very simple jsfiddle that exhibited working behavior [http://jsfiddle.net/3q9Ma/223/], and don't really understand what's different in the call to jstree.create_node(); The success alert doesn't fire, so clearly something is wrong. I've also tried changing the '#' to null, as per the jsfiddle docs, to no avail.

I've created a jsfiddle that exhibits the non-working behavior here: https://jsfiddle.net/z0tjc91v/

Basically, I have a jstree, and want to use the contents of a text box to add or delete an element on the click of a button..

HTML:

<label for="drawerName">Current Drawer:</label>
<input type="text" id="drawerName">
<div id="btnDiv">

  <button id="newDrawerBtn" type="button" class="btn btn-success">
    <span class="glyphicon glyphicon-plus-sign pull-left"></span>New Drawer
  </button>

  <button id="deleteDrawerBtn" type="button" class="btn btn-danger">
    <span class="glyphicon glyphicon-remove-sign pull-left"></span>Delete Drawer
  </button>
</div>
<div id="drawerTreeDiv">
</div>

Javascript:

$(function() {
  var currentFile;

  $('#drawerTreeDiv')
    .on('changed.jstree', function(e, data) {

      currentFile = data.instance.get_node(data.selected[0]).text;
      $('#drawerName').val(currentFile);
    })
    .jstree({
      'core': {
        'multiple': false,
        'check-callback': true,
        'data': [{
          'id': 'path/to/foo',
          'text': 'foo'
        }, {
          'id': 'path/to/bar',
          'text': 'bar'
        }]
      }
    });
  $('#newDrawerBtn').on('click', function() {
    let filename = $('#drawerName').val();
    let pathname = '/path/to/' + filename;
    var newNodeJson = {
      'id': pathname,
      'text': filename

    };
    $('#drawerTreeDiv').jstree().create_node('#', newNodeJson, 'last', function() {
      alert("added");
    });

    //$('#drawerTreeDiv').jstree().refresh();
  });

  $('#deleteDrawerButton').on('click', function() {
    let filename = $('#drawerName').val();
    let pathname = '/path/to/' + filename;
    var node = $('#drawerTreeDiv').jstree().get_node(pathname);

    $('#drawerTreeDiv').jstree().delete_node(node);
    $('#drawerTreeDiv').jstree().refresh();

  });

Upvotes: 0

Views: 540

Answers (1)

betseyb
betseyb

Reputation: 1332

I found the problem. I had check-callback set to true, not check_callback. When I made that change, it worked.

Upvotes: 1

Related Questions