开发者

jquery table sorter - dd/mm/yyyy parser

After searching around, I can't seem to be able to make the first column sort by the UK date format - dd/mm/yyyy Would anyone be willing to help me with this problem? Current js is:

$(document).ready(function() 
{ 
    $("table.tablecontainer").tablesorter({widgets: ['zebra']});

    $("#myTable").tablesorter({ 
               sortList: [[0,开发者_运维百科0]], 
       headers: {
           5:{
              sorter: false
           }
       }
    });
}); 

All help appreciated as this isn't an area I can class myself as proficient in at all!

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
     },
    }});
}); 


I personally use the Jquery tablesorter script from tablesorter.com with the dateFormat- property set to "uk".

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

You are calling the script several times with different options, which confuses the poor library.

Only make one call to tablesorter with all the different options set as above.

I haven't checked the zebra widget, but the rest behaves as it should now.

Good luck with your endeavor :-)


you need to define a parser for this. Something like

// 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 do with some better format checking. I'd recommend defining your parsers in a separate script to the tablesorter plugin so that you can easily upgrade. You would reference the parsers script in a <script> tag after the tablesorter script.

To use would be like

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


this dodges the question, but i might just configure the textExtraction option (see here: http://tablesorter.com/docs/#Configuration)

you could just put a sortable date in a hidden span (class='sort_key' as 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();
  }
}


http://mottie.github.io/tablesorter/docs/

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

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

In previous versions, this option was set as "us", "uk" or "dd/mm/yy". This option was modified to better fit needed date formats. It will only work with four digit years!

The sorter should be set to "shortDate" and the date format can be set in the "dateFormat" option or set for a specific columns within the "headers" option. See the demo page to see it working.

$(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 modified by adding the following (they all do the same thing), set in order of priority (Modified v2.3.1):

  • jQuery data data-dateFormat="mmddyyyy".
  • metadata class="{ dateFormat: 'mmddyyyy'}". This requires the metadata plugin.
  • headers option headers : { 0 : { dateFormat : 'mmddyyyy' } }.
  • header class name class="dateFormat-mmddyyyy". Overall dateFormat option.

In my case I have used

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

for the version.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜