English 中文(简体)
如何在数据加载时添加jquery树视图回调
原标题:How to add jquery treeview callback on data load

我正在使用jquerytreeview插件。有没有办法添加一个回调函数,在我加载数据后调用它?

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

异步树视图中没有回调,但添加自己的回调应该足够容易。AJAX的东西就在这里:

https://github.com/jzaefferer/jquery-treeview/blob/master/jquery.treeview.async.js#L42

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

您可以通过添加<code>settings.dataLoaded</code>并将上面的代码段更改为如下内容来添加回调:

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

我刚刚在success下面添加了两行:。然后你可以这样设置树景:

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

我还没有尝试过,但它应该会起作用,如果没有,这将是一个很好的起点。

你可以访问源代码,没有充分的理由不使用它。而且,没有充分理由不自己添加回调支持,并向Jörn Zaeffer发送一个补丁,将此功能添加到主存储库中。

添加回调真的很有帮助-谢谢。

我发现您可能想要更改树视图异步源代码中的顺序。

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

通过这种方式,异步脚本可以完成对DOM的修改。现在请注意settings.dataLoaded位于sucess:function的末尾。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签