First, I would suggest using the JavaScript object syntax rather than the UL/LI style, because it's more performant (drawback: the UL markup would be rendered, even if JavaScript is disabled).
The trick is, to simply add a custom attribute to the node data (in this example we may call it 'url') like so:
{title: "Google", url: "http://www.google.com"}
Then, in the activation handler, access this attribute like this: dtnode.data.url
.
Excerpt from the sample page ( http://wwwendt.de/tech/dynatree/doc/sample-iframe.html ):
$("#tree").dynatree({
onActivate: function(dtnode) {
// Use our custom attribute to load the new target content:
if( dtnode.data.url )
$("[name=contentFrame]").attr("src", dtnode.data.url);
},
children: [
{title: "Search engines", isFolder: true, expand: true,
children: [
{title: "Google", url: "http://www.google.com"},
{title: "Yahoo", url: "http://yahoo.com"}
]
},
{title: "jQuery", isFolder: true, expand: true,
children: [
{title: "jQuery", url: "http://www.jquery.com"},
{title: "jQuery UI", url: "http://ui.jquery.com"},
{title: "API browser", url: "http://api.jquery.com"},
{title: "dynatree", url: "http://code.google.com/p/dynatree/"}
]
}
]
});
If you want to use an Ajax request, to receive this data from a server in JSON format,
this sample - written in Python - may give a direction:
# Build node list as JSON formatted string:
res = "["
res += "{ 'title': 'Node 1', 'key': 'k1', 'isLazy': true, 'url': 'http://foo.com' },"
res += "{ 'title': 'Node 2', 'key': 'k2', 'isLazy': true, 'url': 'http://bar.com' }"
res += "]"
# Add support for the JSONP protocol:
# This means, if the request URL contains an argument '?callback=xxx',
# wrap the result as 'xxx(result)'
if "callback" in argDict:
res = argDict["callback"] + "(" + res + ")"
# Make sure, content type is JSON:
start_response("200 OK", [("Content-Type", "application/json")])
# Return result (the square brackets are Python / WSGI specific):
return [ res ]
Note that in Ajax/JSON mode you don't have to return hierarchical structures. Instead, you may mark the nodes lazy
, so another request is sent when the nodes are expanded for the first time.