conversoid
conversoid

Reputation: 101

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

Upvotes: 9

Views: 9313

Answers (4)

umeshkumar sohaliya
umeshkumar sohaliya

Reputation: 267

Cause:

Its because when we add any data in table it trigger buildCache() method and save table data in cache variable, but when you delete data from table its not going to make any change in cache variable.

when you sort data than it will clear table and append cached data to table so there is deleted data also present.

Solution:

you can modify onselectstart() function or mousedown event in tablesorter.js

//here tb_35 = id of table 
cache = buildCache($('#tb_35')[0]);

Upvotes: 0

Lukaash
Lukaash

Reputation: 129

The only way I could make it working was to regenerate whole table (remove it and then create again).

$(".resultTablePlaceholder").html('').html('<table id="resultTable">...</table>');
$("#resultTable").tablesorter();

Upvotes: 0

Mottie
Mottie

Reputation: 86463

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');

Upvotes: 16

patman
patman

Reputation: 2838

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.

Upvotes: 1

Related Questions