Items in dhtmlxFolders can be sorted using any of their parameter. To sort items you need to use sortItems method. The main thing you need to do is to define function for items comparison and pass it into that method (as first argument), second argument will be “asc” for ascending sort, “desc” for descending sort.
Items comparison function gets two abstract items as incoming arguments and returns:
For example XML representation of item in dhtmlxFolders is the following:
<item name="[string]" id="[string]"> <size>[numeric value]</size> </item>
Comparison function to sort by name attribute will be the following:
function sortByName(a,b){ var aName = a.data.dataObj.getAttribute("name"); var bName = b.data.dataObj.getAttribute("name"); if(aName>bName) return 1; else if(aName==bName) return 0; else return -1; }
As you can see from example above, we get item XML node using itemObject.data.dataObj construction. Afterwards we can work with it as with XMLDOM node object. To compare numeric value of size node we'll use the following comparison function:
function sortBySize(a,b){ var aSize = parseInt(a.data.dataObj.getElementsByTagName("size")[0].firstChild.nodeValue); var bSize = parseInt(b.data.dataObj.getElementsByTagName("size")[0].firstChild.nodeValue); return aSize-bSize; }
Complete sorting command will be the following:
myFolders.sortItems(sortByName,"asc");//to sort ascending by name myFolders.sortItems(sortBySize,"asc");//to sort ascending by size