Search code examples
javascriptjqueryajaxtreefuelux

How to reaload data in Fuel UX Tree | jQuery


I use Fuel UX Tree framework for loading tree view data.

On first load of document, tree view renders correctly, but when I try to reload tree with new DataSource, I get nothing.

Here is my code example:

Template for tree UI:

<div id="MyTree" class="tree tree-plus-minus tree-no-line tree-unselectable">
    <div class="tree-folder" style="display:none;">
        <div class="tree-folder-header">
            <i class="fa fa-folder"></i>
            <div class="tree-folder-name">
            </div>
        </div>
        <div class="tree-folder-content">
        </div>
        <div class="tree-loader" style="display:none">
        </div>
    </div>
    <div class="tree-item" style="display:none;">
        <i class="tree-dot"></i>
        <div class="tree-item-name">
        </div>
    </div>
</div>

Tree UI init function:

var UITree = function () {

    return {
        //main function to initiate the module
        init: function (el, data) {
            if (typeof el != 'undefined' && typeof data == 'object') {

                var DataSourceTree = function (options) {
                    this._data  = options.data;
                    this._delay = options.delay;
                };

                DataSourceTree.prototype = {
                    data: function (options, callback) {
                        var self = this;
                        var data = $.extend(true, [], self._data);
                        callback({ data: data });
                    }
                };

                var treeDataSource = new DataSourceTree(data);  


                $(el).tree({
                    selectable: false,
                    dataSource: treeDataSource,
                    loadingHTML: '<img src="assets/img/input-spinner.gif"/>',
                });
            }
        }
    };

}();

.

After loading data with Ajax I call init function:

//my data from ajax result
var myData = {
  "data": [
    {
      "name": "some_name",
      "type": "item"
    },
    {
      "name": "some_name_2",
      "type": "item"
    }
  ]
};


// call tree init to render data
UITree.init($('#MyTree'), myData);

Function works without error and renders tree only on first load of page, not on next Ajax call.


Solution

  • Finally I found solution. Maybe it's not the best practice, but this is only way what I found:

    Looks like Fuelux Tree UI doesn't work when element already has assigned data property, so I made this:

    // 1. Clear MyTree wrapper template with: 
    $('#MyTree .tree-item:visible').remove();
    
    // 2. remove assigned data from template element object
    delete($('#MyTree').data().tree);
    
    // 3. Refactor Tree UI
    $('#MyTree').tree({
        selectable: false,
        dataSource: {
            data: function(options, callback) {
                callback(MyData);
            }
        }
    });
    

    I searched all over internet, re-read Fuelux documentation, but nothing is mentioned about re-factoring Tree UI, so if anyone will meet same problem, this solution can help.