href-links to fancytree node including click-event on node

834 views
Skip to first unread message

raahauge

unread,
May 5, 2014, 4:46:21 PM5/5/14
to dyna...@googlegroups.com

I use Fancytree as a menu for my webpage. Each node loads different page content upon clicking, (AJAX).

I would like to have href-links in the page content to individual nodes in the Fancytree, such that when the user clicks the link in the page content, it has the same effect as clicking the relevant Fancytree node, except that the user don't have to search for the relevant node in the Fancytree (lots of nodes).

My idea was to

1) Add click-listeners to the relevant links in the page content when the page content is loaded (I prefix the url with "ft_", so they are easy to separate from other links)
2) Have the listener find the relevant node when clicked (using the Fancytree API)
3) Somehow issue a click-event on the node

I fail to make 3) happen.

My best attempt is below, except the code "set active", instead of "clicking":

// Get all links in page content
var hrefLinks = $("a");

for (var i = 0; i < hrefLinks.length; i++) {

// Get the href of the link
var href = $(hrefLinks[i]).attr('href');

// Compare with prefix indicating, that this is a link to Fancytree
if (href.substring(0, 3) == "ft_") {

// Add listener to link
$(hrefLinks[i]).click(function(e) {
e.preventDefault();

// Find the right node based on the href
var node = $("#tree").fancytree("getTree").getNodeByKey(href.substring(3));

// Arhhhh, I want "click" instead of setActive
node.setActive();
});
}
}

A fix on the click-problem, or perhaps a better overall strategy, would be much appreciated.

raahauge

unread,
May 6, 2014, 5:26:27 AM5/6/14
to dyna...@googlegroups.com
Finally figured out an (embarrassing) easy solution myself: Wrap the Fancytree click-actions in a function, and call this function from my solution above with the needed node-information.

However, still a bit curious on why I could not trigger a click, but nevermind. 

 
Reply all
Reply to author
Forward
0 new messages