EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: devnull on November 09, 2017, 10:42:41 PM



Title: iterate through all levels of tree and sort folders by name
Post by: devnull on November 09, 2017, 10:42:41 PM
Is it possible to iterate through all levels of a tree and sort folders (branches) by text field ??

I looked through the api for the tree but don't see anything ?

Thanks


Title: Re: iterate through all levels of tree and sort folders by name
Post by: jarry on November 10, 2017, 12:49:58 AM
The 'sort' method can be extended as below:
Code:
(function($){
function sortData(target, order){
order = order || 'asc';
var rows = $(target).tree('getRoots');
_sort(rows);
$(target).tree('loadData', rows);
function _sort(rows){
rows.sort(function(r1,r2){
var sortFunc = function(a,b){
return a==b ? 0 : (a>b?1:-1);
};
return sortFunc(r1.text,r2.text) * (order=='asc'?1:-1);
});
for(var i=0; i<rows.length; i++){
var children = rows[i].children;
if (children && children.length){
_sort(children);
}
}
}
}
$.extend($.fn.tree.methods, {
sort: function(jq, order){
return jq.each(function(){
sortData(this, order);
});
}
});
})(jQuery);

Usage example:
Code:
$('#tt').tree('sort', 'asc');
$('#tt').tree('sort', 'desc');