JQuery table sorter - dd / mm / yyyy parser

After searching, it seems I can not sort the first column by date in the UK - dd / mm / yyyy Anyone want to help me with this problem? Current js:

$(document).ready(function() { $("table.tablecontainer").tablesorter({widgets: ['zebra']}); $("#myTable").tablesorter({ sortList: [[0,0]], headers: { 5:{ sorter: false } } }); }); 

All help is appreciated, as this is not an area in which I can classify myself as a professional!

Updated code:

 $(document).ready(function() { $("#myTable").tablesorter({widgets: ['zebra']}); $("#myTable").tableSorter( {dateFormat: "uk"} ); $("#myTable").tablesorter({ sortList: [[0,0]], headers: { // assign the sixth column (we start counting zero) 5:{ // this is header 6 since the headers start at 0 // disable it by setting the property sorter to false sorter: false }, }}); }); 
+4
source share
4 answers

I personally use the jquery tablesorter script from tablesorter.com with the dateFormat parameter - "uk".

  $("#myTable").tablesorter({ widgets: ['zebra'], dateFormat: "uk", sortList: [[0, 0]], headers: { 5: { sorter: false}} }); 

You invoke the script several times with various parameters, which confuses the poor library.

Make only one call to tablesorter with all of the various options above.

I have not tested the zebra widget, but the rest behaves as it should now.

Good luck in your efforts :-)

+8
source

you need to define parser . Sort of

 // add parser through the tablesorter addParser method $.tablesorter.addParser({ // set a unique id id: 'uk-date', is: function(s) { // return false so this parser is not auto detected return false; }, format: function(s) { var date = s.split('/'); return new Date(date[2],date[1],date[0]).getTime(); }, // set type, either numeric or text type: 'numeric' }); 

this could probably be due to a better format check. I would recommend defining your parsers in a separate tablesorter script plugin so you can easily update it. You must reference the script parsers in the <script> after the tablesorter script.

Will use as

 $(function() { $("table").tablesorter({ headers: { 1: { sorter:'uk-date' } } }); }); 
+5
source

this avoids the question, but I can just configure the textExtraction parameter (see here http://tablesorter.com/docs/#Configuration )

you can just put the sorted date in a hidden range (class = "sort_key", as shown below) and set something like:

 textExtraction: function(node) { if($j(node).find('.sort_key').is('span') { return $j(node).find('.sort_key').text(); } else { return $j(node).text(); } } 
0
source

http://mottie.imtqy.com/tablesorter/docs/

Set the date format. Here are the available options. (Changed v2.0.23).

  • "mmddyyyy" (default)
  • "ddmmyyyy"
  • "yyyymmdd"

In previous versions, this option was set to "us", "uk" or "dd / mm / yy". This setting has been changed to better suit date formats. It will only work with four-digit years!

The sorter must be set to "shortDate", and the date format can be set in the "dateFormat" option or set for certain columns in the "headers" option. See the demo page to see how it works.

 $(function(){ $("table").tablesorter({ dateFormat : "mmddyyyy", // default date format // or to change the format for specific columns, // add the dateFormat to the headers option: headers: { 0: { sorter: "shortDate" }, // "shortDate" with the default dateFormat above 1: { sorter: "shortDate", dateFormat: "ddmmyyyy" }, // day first format 2: { sorter: "shortDate", dateFormat: "yyyymmdd" } // year first format } }); }); 

Individual columns can be changed by adding the following (they all do the same thing), set in order of priority (Changed v2.3.1):

  • Data jQuery data-dateFormat = "mmddyyyy".
  • metadata class = "{dateFormat: 'mmddyyyy}. This requires the metadata plugin.
  • Header Header Headers: {0: {dateFormat: 'mmddyyyy}}.
  • header class name class = "dateFormat-mmddyyyy". General option dateFormat.

In my case, I used

 $("#myTable").tablesorter({dateFormat: "uk"}) 

for version.

0
source

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


All Articles