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?

psx
  • 4,040
  • 6
  • 30
  • 59

4 Answers4

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.

Mottie
  • 84,355
  • 30
  • 126
  • 241
  • 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! – psx Nov 18 '11 at 09:49
  • 1
    Ah ok, I've actually forked a copy of [tablesorter over on github](https://github.com/Mottie/tablesorter)... try it out and make sure the bug has been fixed ;) – Mottie Nov 18 '11 at 22:18
  • 1
    +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. – Yahel May 05 '12 at 16:56
  • 1
    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. – psx May 10 '12 at 11:28
  • @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 ;) – Mottie May 11 '12 at 00:52
  • Thank you for this answer as it solved also a problem with dynamic table loading and I just needed .unbind('sorton'). (http://forum.jquery.com/topic/tablesorter-makes-too-many-rows-when-changing-table-contents-with-ajax-but-only-if-sorting-settings#14737000004972324) – e-motiv Jan 08 '14 at 17:45
  • @R-U-Bn my fork of tablesorter now has a method to [destroy](http://mottie.github.io/tablesorter/docs/#destroy) (remove) the plugin. – Mottie Jan 10 '14 at 15:17
  • 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. – e-motiv Jan 13 '14 at 10:10
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

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
Volker Ahlers
  • 191
  • 1
  • 2
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.

Sanjeev
  • 2,607
  • 22
  • 16
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.

Shailesh Jangir
  • 395
  • 2
  • 10