jQuery Dynatree, select a child node by title?

M.Bush picture M.Bush · Mar 7, 2013 · Viewed 7.1k times · Source

I'm using some code to programmatically select a root node (the only one in it) of a dynatree, i.e.

        $("#divDynaTree").dynatree("getRoot").visit(function (node) {
        node.select(true);
    });

I have a second dynatree with multiple "parent" and "children" nodes, and would like to select one of the "children" programmatically when I use a separate event (button click) in the app. I would like to use the title of the child node but am having a hard time finding the correct syntax to do so. I did explore the other dynatree threads on this site and goodle and haven't yet found exactly what I'm looking for (or maybe it was close, but my inexperience caused me to fail to see it). I'm assuming the code will be similar to above, using "visit".. but I'm not sure where to go after that at this time. Any help would be appreciated.

Answer

Amrendra picture Amrendra · Mar 7, 2013

Try this select all child node:

$(function(){
    var inEventHandler = false;
    $("#tree").dynatree({
        checkbox: true,
        selectMode: 2,
        [...]
        onSelect: function(select, dtnode) {
            // Ignore, if this is a recursive call
            if(inEventHandler) 
                return;
            // Select all children of currently selected node
            try {
                inEventHandler = true;
                dtnode.visit(function(childNode){
                    childNode.select(true);
                });
            } finally {
                inEventHandler = false;
            }
        }

or search node by name:

var match = null;
tree.visit(function(node){
    if(node.data.title === "foo"){
        match = node;
        return false; // stop traversal (if we are only interested in first match)
    }
});
alert("Found " + match);