How to add jquery treeview callback on data load

给jquery treeview加个回调函数:

jquery.treeview.async.js:

if(settings.callback){
settings.callback();
}

调用方法:

callback: init_contextmenu

Q:

I am using jquery treeview plugin. Is there any way to add a callback function that is going to be called after I load the data?

 $("#tree").treeview({ url: "/loadtree", callback: my_function // this is what i wanted to do });

A:

$.ajax($.extend(true, { url: settings.url, dataType: "json", data: { root: root }, success: function(response) { child.empty(); $.each(response, createNode, [child]); $(container).treeview({add: child}); } }, settings.ajax));

You can add a callback by adding settings.dataLoaded and changing the above snippet to something like this:

$.ajax($.extend(true, { url: settings.url, dataType: "json", data: { root: root }, success: function(response) { if(settings.dataLoaded) settings.dataLoaded(); child.empty(); $.each(response, createNode, [child]); $(container).treeview({add: child}); } }, settings.ajax));

I just added two lines beneath success:. Then you’d set up the treeview like this:

$("#tree").treeview({ url: "/loadtree", dataLoaded: my_function });

I haven’t tried this but it should work and if not, it will be a good starting point for getting it going.

You have access to the source code, there’s no good reason not to use it. And, there’s no good reason not to add the callback support yourself and send Jörn Zaefferer a patch to get this functionality added to the main repository.

Adding the callback was really helpful – Thanks .

I found that you might want to change the order inside the treeview async source code.

 $.ajax($.extend(true, { url: settings.url, dataType: "json", data: { root: root }, success: function(response) { child.empty(); $.each(response, createNode, [child]); $(container).treeview({add: child}); if(settings.dataLoaded) settings.dataLoaded(); } }, settings.ajax));

This way the async script can finish modifying the DOM. Notice now settings.dataLoaded comes at the end of sucess:function.

来源:http://stackoverflow.com/questions/4905101/how-to-add-jquery-treeview-callback-on-data-load

About 智足者富

http://chenpeng.info

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>