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
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');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With