Search code examples
jqueryjquery-uijquery-pluginsjqtree

Updating jqTree after initialization?


I'm new with jqTree and I'd like to reload the tree after ajax call. I have something like this :

$("select").change(function(){
    var url = 'lab.json';

    if ($(this).val() === 'RAD') {
        url = 'rad.json';
    }

    $.get(
        url, 
        function(jsonData) {
            $("#treedata").tree({data: jsonData});
        }, 
        "json"
    );
});

The first call is working but for the next ones the tree doesn't update with the new data.

Any idea how to update the tree after initialization ?

Thanks

EDIT :

I found a solution but it's not perfect. If someone has a better solution let me know :)

 $("#treebox").empty().append('<div id="treedata"></div>');
        $("#treedata").tree({
            data: jsonData
        });

I have to remove the generated content by jqTree using $.empty() and then initialize jqTree each time I want to update the tree with new data.


Solution

  • You can use the function 'loadData' to load new data in the tree. This function is supported in the latest version of jqTree (0.6).

    For example:

    // Assuming the tree exists
    var new_data = [
        {
            label: 'node1',
            children: [
                { label: 'child1' },
                { label: 'child2' }
            ]
        },
        {
            label: 'node2',
            children: [
                { label: 'child3' }
            ]
        }
    ];
    
    $('#tree1').tree('loadData', new_data);
    

    Also see http://mbraak.github.com/jqTree/#functions-loadData