JQuery Tablesorter clear table information
Asked Answered
B

4

9

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

Bezonian answered 15/12, 2012 at 17:1 Comment(0)
P
16

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');
Photocell answered 16/12, 2012 at 15:30 Comment(3)
how can I preserve the sorting preferences that way?Southernly
@Southernly Try out my fork of tablesorter, an update event can be flagged to resort the contents on update.Photocell
Thank you very much. This was the ONLY thing that would work for meSudden
P
1

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.

Playpen answered 15/12, 2012 at 17:32 Comment(0)
S
0

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();
Stickup answered 15/10, 2013 at 14:36 Comment(0)
K
0

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]);
Koala answered 1/8, 2017 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.