Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Tablesorter clear table information

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

like image 821
conversoid Avatar asked Dec 15 '12 17:12

conversoid


2 Answers

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');
like image 67
Mottie Avatar answered Oct 23 '22 20:10

Mottie


I see no reason why it shouldn't be working. Is it working without the tablesorter plugin?

Why are calling tablesorter() on table but try to delete rows from #table? Typo?

Try logging the jQuery objects for $('#table'), $('#table tr') and $('#table tr:gt(0)') and see if everything is correct there.

like image 21
patman Avatar answered Oct 23 '22 21:10

patman