Search code examples
javascriptjqueryajaxtablesorter

JQuery Tablesorter ajax update


I have problem with updating table. I use plugin of JQuery Tablesorter. Sorting works, but pagination not working.

$(function () {
    $("#myTable").tablesorter();
    $('#topicT').on('click', 'a', function () {
        var href = $(this).attr('href');
        var id = href.replace('#', '');
        $.ajax({
            url: 'faqs',
            type: 'POST',
            data: {topicId: id},
            success: function (response) {
                var html = '';
                $.each(response, function (key, val) {
                    html += '<tr><td>' + val.question + '</td></tr>'
                });
                $('#myTable tbody').empty().append(html);
                $('#myTable').trigger('update');
                var sorting = [
                    [0, 0]
                ];
                $('#myTable').trigger('sorton', [sorting]);
            }
        });
    });

});

When I change

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

to

$("#myTable").tablesorter({widthFixed: true, widgets: ['zebra']}).tablesorterPager({container: $("#pager")});

table not working.

Error: Cannot read property 'length' of undefined. And data don't load to the table. Can somebody help my to solve this problem?


Solution

  • Problem was solved. Added jquery-migrate-1.2.1.js. Working code:

    $(function () {
    $("#myTable").tablesorter();
    $('#topicT').on('click', 'a', function () {
        var href = $(this).attr('href');
        var id = href.replace('#', '');
        $.ajax({
            url: 'faqs',
            type: 'POST',
            data: {topicId: id},
            success: function (response) {
                var html = '';
                $.each(response, function (key, val) {
                    html += '<tr><td>' + val.question + '</td></tr>'
                });
                $('#myTable tbody').empty().append(html);
                $('#myTable').trigger('update');
                $("#myTable").tablesorter({widthFixed: true, widgets:['zebra']}).tablesorterPager({container: $("#pager")});
                var sorting = [
                    [0, 0]
                ];
                $('#myTable').trigger('sorton', [sorting]);
            }
        });
    });
    
    });