JQuery Tablesorter clear table information

conversoid picture conversoid · Dec 15, 2012 · Viewed 8.7k times · Source

I've created a jquery table that from time to time needs to be cleared and the re-populated, my clear method is this:

//Clear table content before repopulating values
$('#table tr:gt(0)').remove();

Now i'm trying to use tablesorter to sort a specific column, but my problem is that when I enable the tablesorter:

//Initialize tablesorter
$("table").tablesorter();

The table clearing method is not working anymore, it just keeps adding the new data with the old data, creating a lot of repeated information.

Please help

Answer

Mottie picture Mottie · Dec 16, 2012

To update tablesorter, an "update" needs to be triggered before it will update its cache

$('table').trigger('update');

You could also use the built-in function to clear the table

$.tablesorter.clearTableBody( table );

Here is the code combined from this demo

var $table = $('table');

// use built in clear function
$.tablesorter.clearTableBody( $table[0] );
$table
    .append('<tr><td>george</td><td>papard</td><td>68</td><td>19.99</td><td>55%</td><td>+3</td></tr>')
    .trigger('update');