Home > OS >  Is it possible to collapse all sim-tree nodes programatically?
Is it possible to collapse all sim-tree nodes programatically?

Time:07-06

I'm using the sim-tree tree view plugin (https://github.com/linjingming/sim-tree) to display parent - child list. The items/checkboxes to be loaded as checked are fetched programmatically. All 'tree nodes' that have at least one 'checked = true' item loads as expanded/open. Please see the image below. The node named 'Agder' have all items checked, and as you can see the node is expanded. I would like the node to be collapsed when the plugin is done loading. Expanded nodes

Is there a way to open the sim-tree view with all nodes collapsed even if there are items in the nodes which are checked? Please see my code below:

function getLocations() {
    console.log("function get locations @start");
    $.ajax({
        url: '../../location/get_new_locations.php', data: "", dataType: 'json',
        success:
            function (response) {
                var district = response;
                var list = [];
                for (var i in district) {
                    var districtrow = district[i];
                    var listcontent = [{
                        "id": districtrow[0],
                        "pid": districtrow[1],
                        "name": districtrow[2],
                        "checked": districtrow[3]

                    }];
                    $.each(listcontent, function (index, item) {
                        list.push(item);
                    });
                }
                var tree = simTree({
                    el: '#treez',
                    data: list,
                    check: true,
                    linkParent: true,
                    onClick: function (item) {
                        console.log(item)
                    },
                    onChange: function (item) {
                        console.log(item)
                        var formData = JSON.stringify(item);
                        //console.log(formData)

                    },
                    done: function () {
                        //nothing here yet
                    }

                });

            }, // end response success
        error: function (jqXHR, exception) {
            var msg = '';
            if (jqXHR.status === 0) {
                //msg = 'Not connect.\n Verify Network.';
            } else if (jqXHR.status == 404) {
                msg = 'Requested page not found. [404]';
            } else if (jqXHR.status == 500) {
                msg = 'Internal Server Error [500].';
            } else if (exception === 'parsererror') {
                msg = 'Requested JSON parse failed.';
            } else if (exception === 'timeout') {
                msg = 'Time out error.';
            } else if (exception === 'abort') {
                msg = 'Ajax request aborted.';
            } else {
                msg = 'Uncaught Error.\n'   jqXHR.responseText;
            }
            console.log(msg);
            console.log(jqXHR.responseText);
        }
    }); //end ajax
}; // end function

CodePudding user response:

After the document is loaded, trigger a click() on all expanded nodes to collapse them:

$(document).ready(function(){
  $('#tree').find('.sim-icon-d').each(function(){
      $(this).click()
  })
});
  • Related