jQuery tablesorter plugin does not work after AJAX call

Deadly 提交于 2019-12-06 00:33:54

It's unclear about the mechanism you're using to make the AJAX call but if it's the ASP.NET UpdatePanel, then you will need to rebind your jQuery events after the AJAX call is complete.

Add the following to your script

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(function(sender, args) {
    // Code to rebind your UI
});

Note: only works if you're using ASP.NET AJAX

The new DOM elements are not binded to the JavaScript events. jQuery handles a similar problem using it's 'live' function. Once the AJAX call has completed, rerun the javascript in document.ready().

I would wrap the tablesorter function in it's own function.

Then whenever you need to re run it - just call it again.

$(document).ready(function () {
    function resortTable(){ 
          $("#myTable").tablesorter({
            widthFixed: true,
            widgets: ['zebra'],
            headers: {
                0: {
                    sorter: false
                }
            }
        }).tablesorterPager({
            container: $("#pager")
        });
    }

        $("#tag").change(function() {
            resortTable();
        });
});
Alex King

The problem is that you're calling the $('#myTable').trigger("update"); code when the combo box changes, instead of when you get a response from your AJAX request. If you're using ASP.NET AJAX, try the code ericphan posted. If you're using jQuery AJAX, try something like this:

$.get('http://site.com/request-address', function(data) {

    // Code you're using to replace the contents of the table

    // Code to rebind the tablesorter
    $('#myTable').trigger("update");
    $("#myTable").tablesorter();
});

That way, you're rebinding the tablesorter to the new table contents, not the old contents which are about to be replaced.

i would look into using the livequery plugin for this... it works miracles

http://docs.jquery.com/Plugins/livequery

I know this is an old post, but maybe my answer will help someone else googling this same problem. I fixed this problem by calling

$('#myTable').tablesorter();

again right after my AJAX call.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!