JsTree: How to sort jstree nodes with folders at the top
Asked Answered
O

2

10

I use the plugin Jstree to draw a tree of folders ans files.

I want to get the list of folders at the top then the list of files (the list of folders and files must be sorted in alphabetical order).

There is my function of initilization of the tree:

$('#jstree_demo_div').jstree({ 
    'core' : {
        'data' : [

            {"id":"index_0","text":"test_folder","parent":"#","icon":""},
            {"id":"index_1","text":"vide","parent":"index_0","icon":""},
            {"id":"index_2","text":"05nesf-sdfdgd.mp4","parent":"index_1","icon":"fa fa-film"},
            {"id":"index_3","text":"naissance-d-une-fleur-ouwzp9me-41.mp4","parent":"index_0","icon":"fa fa-film"},
            {"id":"index_4","text":"za05nesfsdfsdg.mp4","parent":"index_0","icon":"fa fa-film"},
            {"id":"index_5","text":"ddd","parent":"#","icon":""},
            {"id":"index_6","text":"05nes-ibw6q9me-41.mp4","parent":"index_5","icon":"fa fa-film"},
            {"id":"index_7","text":"tom-jerry-soundscape-ttar8gme-41.mp4","parent":"#","icon":"fa fa-film"},
            {"id":"index_8","text":"aaes-qmc8q-9me-41.mp4","parent":"#","icon":"fa fa-film"},
            {"id":"index_9","text":"bb05nes.mp4","parent":"#","icon":"fa fa-film"}
        ]
    },
    'plugins' : ['sort','types'],
    'sort' : function(a, b) {
        //What is the function of sorting
    },
});

the result of my initialisation : Tree

What function of sorting I need to use?

Outofdate answered 9/12, 2016 at 13:25 Comment(0)
C
15

You can sort by icon and after by text :

'sort' : function(a, b) {
        a1 = this.get_node(a);
        b1 = this.get_node(b);
        if (a1.icon == b1.icon){
            return (a1.text > b1.text) ? 1 : -1;
        } else {
            return (a1.icon > b1.icon) ? 1 : -1;
        }

Here is a jsfiddle

Claustrophobia answered 9/12, 2016 at 14:7 Comment(3)
Amazing function just works as a charm! What should I adjust on if I want the Folders to be at the Bottom, please?System
Got it, by changing to this a1.icon < b1.icon. Thanks again! ^System
@Alexandre subfolders get ignored by these sort function. Do you have a fix to sort also subfolders?Tc
P
0

Extending answer by Alexandre - in case you want folders on top (default icon) and rest of nodes alphabetically:

 sort: (a, b) => {
   a1 = $('#jstree_demo_div').jstree(true).get_node(a); // default change the selector
   b1 = $('#jstree_demo_div').jstree(true).get_node(b);
                
   if (a1.icon === true || b1.icon === true) { // if either of the nodes is a folder (no icon)
     if (a1.icon === true && b1.icon !== true) { // a is folder, b is leaf, we push folder up
        return 1;
     }
                     
     if (a1.icon !== true && b1.icon === true) { // a is leaf, b is folder, we push folder up
       return 1;
     }
                     
     return (a1.text > b1.text) ? 1 : -1; // a is folder, b is folder -> alphabetically
   }
   else {
     return (a1.text > b1.text) ? 1 : -1; // a is leaf, b is leaf -> alphabetically
   }
 }

jstree uses true as a default value for icon when no icon is provided. With this sorting setup your leafs can have different icons and will still be sorted correctly (folders alphabetically, then leafs alphabetically).

Phila answered 7/4, 2021 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.