Question

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.

Was it helpful?

Solution

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);

OTHER TIPS

You might be looking for

$("#tree").dynatree("getTree").activateKey("KeyName")

Which will activate the node from an external event using its key. This is from dynaTree documentation second example under "Programming dynatree"

I didn't look for a way to find node by its name instead of key, there might be a better way of doing it, but I think you can use visit to find the key and then activate it like this.

function ActivateNodeByTitle(nodeTitle){
    var $tree = $("#tree").dynatree("getTree")
    $tree.visit(function(node){
        if(node.data.title === nodeTitle){
            $tree.activateKey(node.data.key)
            return false; // stop traverse
        }
    });
    return false
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top