views:

680

answers:

5

I'm trying to use an AJAX request to load some information into a table using JQuery. I can load the content easily but I am having problems adding event handlers to apply style on the loaded content.

I am applying a striping widget to the table. However when my AJAX request comes back and my information is appended to the table body it does not have the style applied.

The JQuery tutorial mentions using the load event to handle this, but I can't get it to work.

Any help would be appreciated.

$(document).ready(function(){
    $("#cowTable").tablesorter();
    addTableStriping();
});

function addTableStriping(){
    $("#cowTable").tablesorter({
        // striping looking
        widgets: ['zebra']  
    });
};

$(function(){
    $("#loadCowsButton").click(function(){
        $.post("loadCows.php", "scottm", function(xml) {
            $("#cowTable tbody").append(xml);
        });
    });
});

A: 

You might want to try the Livequery Plugin

Live Query utilizes the power of jQuery selectors by binding events or firing callbacks for matched elements auto-magically, even after the page has been loaded and the DOM updated.

Dave Kiss
+1  A: 

THIS is what you should do:

Found it on the tablesorter website.

The long and short of it is that you should call $( "#cowTable" ).trigger( "update" ); after appending the data.

Jacob Relkin
Which element do I bind the live to? And which event should it listen for?
Scottm
I updated my answer.
Jacob Relkin
Thanks, I'll try this later and I'll get you know how I get on.
Scottm
I've tried this and it doesn't seem to work. It sounds like the correct thing to do, but the striping effect does not seem to be updated. I will continue looking at the tablesorter examples in the meantime.
Scottm
A: 

Why not just call it in the AJAX callback?

$(function(){
    $("#loadCowsButton").click(function(){
        $.post("loadCows.php", "scottm", function(xml) {
            $("#cowTable tbody").append(xml);
            addTableStriping(); // <-- here
        });
    });
});
nickf
This does't work, I've already tried it.
Scottm
@Scottm well, it should work... what happened when you tried it?
nickf
A: 

this should do it ..

$(function(){
    $("#loadCowsButton").click(function(){
        $.post("loadCows.php", "scottm", function(xml) {
            $("#cowTable tbody").append(xml);
            $("#cowTable").trigger( "update" );
        });
    });
});
Gaby
Maybe it would be wise to look at other people's answers before writing your own.
Jacob Relkin
Point well taken. I had opened the window and until I answer, you (and the others) had already answered ... Didn't delete it as it was an all-inclusive answer (full-code)
Gaby
A: 

I've found a solution for this. You need to trigger "applyWidgets" after updating. Thanks for all the help from everyone, I wouldn't have found this out without your help.

$(function(){
    $("#loadCowsButton").click(function(){
        $.post("loadCows.php", "scottm", function(xml) {
            $("#cowTable").append(xml);
            $("#cowTable").trigger("update");
            $("#cowTable").trigger("applyWidgets")
        });
    });
});
Scottm