JQuery tablesorter: how to disable sorting by column using a class instead of "embedded JSON"?

I am using jQuery tablesorter plugin . I know how to disable column sorting using jQuery metadata plugin:

<th class="{sorter: false}">Don't sort me</th> 

But I would prefer to do this by setting the class, so I don't need to use an additional plugin. Also, I think it's better to remember the class name than to remember this JSON syntax. How can I do the same with this syntax:

 <th class="not-sortable">Don't sort me</th> 
+6
source share
4 answers

I think the only way to get this to work is to change the source code of the plugin.

In jquery.tablesorter.js line 483:

 function checkHeaderMetadata(cell) { if (($.metadata) && ($(cell).metadata().sorter === false)) { return true; }; return false; } 

Change this code to:

 function checkHeaderMetadata(cell) { if ((($.metadata) && ($(cell).metadata().sorter === false)) || $(cell).hasClass("not-sortable")) { return true; }; return false; } 

Now the checkHeaderMetadata function also returns true if the cell has a class called not-sortable .

+11
source

You do not need to change the source of the plugin. Assuming your th class is not sortable, called nosort :

 function setupTablesorter() { $('table.tablesorter').each(function (i, e) { var myHeaders = {} $(this).find('th.nosort').each(function (i, e) { myHeaders[$(this).index()] = { sorter: false }; }); $(this).tablesorter({ widgets: ['zebra'], headers: myHeaders }); }); } 
+17
source

I agree, inline JSON was weird. If you are using tablesorter v2.3 or later, you can actually use data roles to do the same β€” and you don’t need to use the metadata plugin:

 <th data-sorter="false">...</th> 

Significantly cleaner. Please note that this requires jQuery.

For more information and a demo: http://mottie.imtqy.com/tablesorter/docs/example-parsers-jquery-data.html

All the best!

  • Ben
+15
source

Starting with version 2.0.x , you can decide upon initialization, not in the column.

Add a property called headers and imagine the sorter property sorter with the value false . Count columns start at 0 .

 jQuery('#element').tablesorter({ headers: { // first column 0: { sorter: false }, // third column 2: { sorter: false } } }); 

An example taken from docs .

+2
source

Source: https://habr.com/ru/post/892344/


All Articles