Reload entire Fancytree

Found your post while searching for a solution myself, and could not find any on the web.

But I just thought about a little trick and it did work so in case it helps you or someone else.

Just remove the tree div, then put it back and load it again, something like that:

$("#tree").remove();
$("#tree_container").append('<div id="tree"></div>');
get_tree ();

You can also change the tree source without change other options using the following code:

$('#t-board').fancytree('option', 'source', myJsonData);

Remember that myJsonData must be a valid JSON data like:

var myJsonSource = [
    {title: "Node 1", key: "1"},
    {title: "Folder 2", key: "2", folder: true, children: [
      {title: "Node 2.1", key: "3", myOwnAttr: "abc"},
      {title: "Node 2.2", key: "4"}
    ]}
  ];

https://github.com/mar10/fancytree/wiki#configure-options


You can not re-initialize the tree. But you can update the tree options or reload it with new source option.

  • Reload the tree with new source option

    var treeOptions = {...}; // initial options
    $('#t-board').fancytree(treeOptions);
    $('#combobox').change(function () {
    var id = $('option:selected', this).val();
    
      var newSourceOption = {
        url: 'get_treedata.php',
        type: 'POST',
        data: {
          id: id
        },
        dataType: 'json'
      };
    
      var tree = $('#t-board').fancytree('getTree');
      tree.reload(newSourceOption);
    });
    
  • Add or replace other tree options

    var treeOptions0 = {...}; // initial options
    var treeOptions1 = {...}; // other tree options
    var treeOptions2 = {...};
    $('#t-board').fancytree(treeOptions0);
    $('#combobox').change(function () {
    
      var id = $('option:selected', this).val();
    
      if(id === '1'){
        $('#t-board').fancytree('option', 'selectMode', treeOptions1.selectMode);
        $('#t-board').fancytree('option', 'renderNode', treeOptions1.renderNode);
        $('#t-board').fancytree('option', 'icons', treeOptions1.icons);
        //...
      }else if(id === '2'){
        $('#t-board').fancytree('option', 'selectMode', treeOptions2.selectMode);
        $('#t-board').fancytree('option', 'renderNode', treeOptions2.renderNode);
        $('#t-board').fancytree('option', 'icons', treeOptions2.icons);
        //...
      }
    });