Shaggy
Shaggy

Reputation: 5800

sort all nodes in dynatree in alphabetical order

How do i sort all sub subsequent child node in jquery dynatree http://wwwendt.de/tech/dynatree/index.html ?

Using this line $("#categoryTree").dynatree("getRoot").sortChildren(compare, false); i am able to sort first level nodes but elements inside that it needs sorting as well.

var compare = function (a, b) {
    a = a.data.title.toLowerCase();
    b = b.data.title.toLowerCase();
    return a > b ? 1 : a < b ? -1 : 0;
};

var tree = $("#categoryTree").dynatree({
    checkbox: true,
    selectMode: 3,
    children: data,
    onPostInit: function (isReloading, isError) {       
        $("#categoryTree").dynatree("getRoot").sortChildren(compare, false); 

        $(".dynatree-selected").each(function () { 
            var node = $.ui.dynatree.getNode(this);
            node.visitParents(function (node) {
                node.toggleExpand();
            }, true);
        });

        $(".dynatree-partsel:not(.dynatree-selected)").each(function () { 
            var node = $.ui.dynatree.getNode(this);
            node.visitParents(function (node) {
                node.toggleExpand();
            }, true);
        });
    }
});

Upvotes: 1

Views: 782

Answers (1)

Shaggy
Shaggy

Reputation: 5800

This worked for me

var root = $("#categoryTree").dynatree("getRoot");

for (var i = 0; i < root.getChildren().length; i++) {
       root.getChildren()[i].sortChildren(compare, false);
}

Upvotes: 0

Related Questions