jstree 3.0.0 sort parent node after rename

1,616 views
Skip to first unread message

David Mann

unread,
Mar 3, 2014, 8:05:44 AM3/3/14
to jst...@googlegroups.com
I've bound a function to the rename_node event. I can use this event to trigger an ajax request to update the server as expected, but how can I sort the node's parent so that the tree instance displays correctly. When I change a node's text to "z" from "a", it stubbornly stays in the same place. I've included the sort plugin.

My onRename function is triggered by the rename_node event. I get a reference to the relevant node from the data parameter

onRename = function(ev, data) {
    var node = data.node;
...

Then after the ajax request is done I get the parent node using the get_parent method on the treeInstance and attempt to sort.

var parent = treeInstance.get_parent(node);
                  treeInstance.sort(parent, true);

The parent node remains unchanged. How can I re-sort the parent node of a node which I have renamed?

Ivan Bozhanov

unread,
Mar 3, 2014, 9:04:19 AM3/3/14
to jst...@googlegroups.com
That is odd - I have no problem with the sort plugin - nodes are properly reordered after a rename, you do not need to invoke sort manually - show me your config - maybe there is some error in the config. Also check your sorting function - if it is custom it may be a bug in the sort function? All I do is this and have no problems:

$('#tree').jstree({
            'core': {
                'check_callback' : true,
                'data': [ { text : "root" } ]
            },
            "plugins" : ["contextmenu","sort"]
        })

I create two nodes in the root (using contextmenu) named "a" and "b", when I rename "a" to "z" it is moved to the bottom automatically.

Best regards,
Ivan

David Mann

unread,
Mar 23, 2014, 12:27:46 PM3/23/14
to jst...@googlegroups.com
It's my sort function. When I comment out my sort function from the tree config object, rename works as expected. When I assign my custom sort function, it sorts exactly the way I want on load, but after renaming it doesn't work. Here's my sort function:
        sort = function(a, b) {
            var instance = this, nodeA, nodeB, typeA, typeB, textA, textB, retVal;

            nodeA = instance.get_node(a);
            nodeB = instance.get_node(b);
            typeA = nodeA.original.type;
            typeB = nodeB.original.type;
            textA = nodeA.original.text.toLowerCase();
            textB = nodeB.original.text.toLowerCase();
            retVal = 0;
            // for a sort function:
            // if you return 1, a comes first
            // if you return -1, b comes first
            switch (typeA) {
                case 'folder':
                    if (typeB === 'file') {
                        retVal = -1;
                    } else {
                        retVal = textA > textB ? 1 : -1
                    }
                    break;
                case 'file':
                    if (typeB === 'file') {
                        retVal = textA > textB ? 1 : -1
                    } else {
                        retVal = 1;
                    }
                    break;
            }
            return retVal;
        };

I have two types of node, folder or file. As with Windows explorer, I want folders to display in alphabetical order, followed by files in alphabetical. Can you see why it works beautifully on load but doesn't re-sort on rename?

David Mann

unread,
Mar 23, 2014, 12:42:33 PM3/23/14
to jst...@googlegroups.com
Got it! It's these two lines:
            textA = nodeA.original.text.toLowerCase();
           textB = nodeB.original.text.toLowerCase();
After rename the node.original.text still seems to have the old name, but node.text has the new name. I've changed those lines to:
            textA = nodeA.text.toLowerCase();
           textB = nodeB.text.toLowerCase();

and it works as expected.

Thanks for the continued support in finding my bugs.

Ivan Bozhanov

unread,
Mar 24, 2014, 3:21:58 AM3/24/14
to jst...@googlegroups.com
Sorry I was not here earlier to help :) glad you worked it out :)
Reply all
Reply to author
Forward
0 new messages