Remove jQuery tablesorter from table
Asked Answered
R

4

13

I am using the jQuery tablesorter (http://tablesorter.com).

After being applied to a table by $('#myTable').tablesorter(), how can I remove it again from the table?

Repossess answered 17/11, 2011 at 17:24 Comment(0)
H
29

There isn't a built-in function to do this, but you could remove the class names and event bindings to stop its functioning... try something like this:

$('table')
 .unbind('appendCache applyWidgetId applyWidgets sorton update updateCell')
 .removeClass('tablesorter')
 .find('thead th')
 .unbind('click mousedown')
 .removeClass('header headerSortDown headerSortUp');

The above won't work if you have the pager plugin running.

Himalayas answered 18/11, 2011 at 4:43 Comment(8)
I see what the above does, but the reason I was looking to remove the tablesorter was due to a known bug in tablesorter. If the table has a new row added, and the trigger("update") is called the sort does not work correctly. The suggested workaround is to reapply .tablesorter() to the table. I was concerned if this might cause more memory to be used in comparison to removing/unbinding the tablesorter and then readding it. What do you think? Hope this makes sense!Repossess
Ah ok, I've actually forked a copy of tablesorter over on github... try it out and make sure the bug has been fixed ;)Himalayas
+1, plus a bounty coming soon. This fixed a really hard to pinpoint bug where the plugin was restoring deleted data from the table when the sorting function was called again.Osber
yahelc, glad the question helped you in some way. Just to let you know, I stopped using tablesorter due to its speed mainly. I am using datatables (datatables.net) instead - it is so much better. I completely recommend it.Repossess
@psynnott I've modified my fork of tablesorter to be about 20% faster than the original. Personally, I like tablesorter because it's only about 30k in size where Datatables is something like 200k. But I guess I'm a bit biased ;)Himalayas
Thank you for this answer as it solved also a problem with dynamic table loading and I just needed .unbind('sorton'). (forum.jquery.com/topic/…)Rhinoplasty
@R-U-Bn my fork of tablesorter now has a method to destroy (remove) the plugin.Himalayas
Thanks, I came across your fork, Mottie, but I was looking for the smallest footprint of a sorter, and I didn't decided tot take yours since it is a bit less than double the size.Rhinoplasty
S
19

tablesorter2.0

$("#table").trigger("destroy");

or if you just in need to update all after appending new thead :

$("#table").trigger("updateAll");

-> http://mottie.github.io/tablesorter/docs/index.html

Sorci answered 31/5, 2013 at 12:50 Comment(0)
I
6

Latest version of table sorter library provides Destroy method

From version 2.16 destroy() method has been added in table sorter library, use this method to remove tablesorter from the table.

Iridectomy answered 16/6, 2014 at 6:9 Comment(0)
P
3

use the function given below onclick event of remove shorting element

function removeTableShorter(){
$("#myTable").tablesorter({ 
headers: {
 0: {sorter: false},
 1: {sorter: false},
 2: {sorter: false},
 3: {sorter: false},
 4: {sorter: false},
 5: {sorter: false}
}
});
$('#myTable th').removeAttr('class');}

u may increase the number of headers according the number of columns of table.

Psoriasis answered 18/11, 2011 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.