jquery tablesorter + ajax div content update problem

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 10:30:52
Click Upvote

After loading the result, you need to do $("#table").tablesorter() once more to re-sort it. Also, rather than writing your ajax code by hand, use $.get or $.post from jquery

Instead of calling .tablesorter() again, you can trigger an update instead, without any of the overhead of calling .tablesorter():

("#table").trigger("update");

I've used this successfully in my own project. You can make the trigger() call in your :success handler.

HTH

Your original issue was that Live Query can only detect changes to the document that started with a jQuery call.

Directly setting innerHTML will not cause it to fire. Changing that line to $("#"+divid).html(xmlHttp_one.responseText) would have solved your problem.

I'm glad to hear that you found a solution! Be aware, however, that Live Query has to scan the document every time it is modified — which is convenient but comes with a big performance hit. It would be better to put the call to tablesorter() in your jQuery.ajax(success:) function.

I had the same problem and found this method.

$("#table tbody tr").addClass("to-delete");
$("#table tbody").append(data);
$("#table").trigger("update");
$("#table").trigger("appendCache");
$("#table").trigger("sorton",[[[2,1],[0,0]]]);
$("#table tbody tr.to-delete").remove();
$("#table").trigger("update");

It's not very pretty but it works!

Found a solution by using jQuery .ajax function. much easier, and works perfectly.

As mentioned using the jquery AJAX call is the best way to go :P but I found the post to be a bit vague for newbies so here's the code that i used in my project:

    $('input.user').click(function() {
    var getContact = $(this).val();
    $.ajax({
        url: 'contact_table.php',
        data: 'userID='+getContacts,
        success: function(result) {
            $('#UserContactTable').append(result);
            $("#contact-list").tablesorter();
        }
    });
});

Use the ajaxStop function and the code will run after the ajax call is complete.

$("#DivBeingUpdated").ajaxStop(function(){ 
    $("table").tablesorter()
});

I am quite new in java/ajax programming, but have exactly the same problem - when I update the content of a div (using ajax) with my table, tablesorter does not function. If I load the table directly to the very first page (not into a div), tablesorter works perfectly.

So, I would be greatful if you can explain in more details how exactly you modified your code to solve it.

Thanks, Bojan

alieNeha

The answer is irrelevant to the question asked but it might help someone.

In case of loading table contents via AJAX call the function tablesorter() must be called after successful execution of AJAX call. Code that explains the same -->

        $(document).ready(function(){
        //once the document is loaded make the AJAX call to required url
        $.ajax({
            url : 'nutrition.xml',  //I have accessed nutrition.xml file
            type : 'GET',
            dataType : 'xml'    //return type is xml
        })
        .done(function(xml){
            //after successful call
            /*here i have created an html string but,
            one call also use appendTo() like : 
            $("<thead></thead>").appendTo("table"); and so on.. */

            var str = "<thead><tr><th>Name</th><th>Calories</th></tr></thead><tbody>";

            //loop through each element of xml filer
            $(xml).find('food').each(function(i){
                var name = $(this).find('name').text();
                var calories = $(this).find('calories').attr('total');
                //append to string
                str = str + "<tr><td>"+name+"</td><td>"+calories+"</td></tr>";
            });
            str = str + "</tbody>";
            //set html for <table>
            $("table").html(str);

            //the main part call tablesorter() once contents are set successfully
            $("table").tablesorter({debug: true}); 
        })
        .fail(function(xhr,status,errorThrown){
            //on ajax call failure
            alert("An error occurred while processing XML file.");
        });         
    });

The contents of nutrition.xml file :

<?xml version="1.0"?>
    <nutrition>
    <food>
        <name>Avocado Dip</name>
        <calories total="110" fat="100"/>
    </food>
    <food>
        <name>Bagels, New York Style </name>
        <calories total="300" fat="35"/>
    </food>
    <food>
        <name>Beef Frankfurter, Quarter Pound </name>
        <calories total="370" fat="290"/>
    </food>
    </nutrition>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!