Problem with sorting dates with jquery tablesorter

后端 未结 8 725
终归单人心
终归单人心 2021-02-02 12:18

I am using tablesorter plugin to sort my tables in an MVC .NET App. Most of my columns are strings and I\'m having no problems with them. Neither with the numeric ones. The thin

相关标签:
8条回答
  • 2021-02-02 12:32

    I got the same problem, and I added a custom parser called datetime:

    $.tablesorter.addParser({
        id: "datetime",
        is: function(s) {
            return false; 
        },
        format: function(s,table) {
            s = s.replace(/\-/g,"/");
            s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$2/$1");
            return $.tablesorter.formatFloat(new Date(s).getTime());
        },
        type: "numeric"
    });
    

    Then you just need to apply that format to the columns you want, as Gabe G exposed (For example to assign this sorter to the first column you should do the following:

    $("#mytable").tablesorter( 
        {   dateFormat: 'dd/mm/yyyy', 
            headers: 
                {
                    0:{sorter:'datetime'}
                } 
        } ); 
    
    0 讨论(0)
  • 2021-02-02 12:32

    To be honest the simplest solution for me was, as compsmart said, adding some hidden text in front of the actual date.

    • dateFormat: 'uk' was not working for me, maybe because my date format is again different
    • http://tablesorter.openwerk.de/ involves modifying the CSS, first I don't understand why and second the effort is bigger than simple adding hidden text in front of the date.

    I like the KISS solution from compsmart!

    0 讨论(0)
  • 2021-02-02 12:44

    There exist an update for jquery tablesorter plugin.

    According to the locale of the your application, you can sort the dates by this update.

    You can view the update of the tablesorter by following the below link.

    http://tablesorter.openwerk.de/

    0 讨论(0)
  • 2021-02-02 12:45

    You can also add a hidden span tag before your date in numerical format (yyyymmdd). This text will come first and be used for sorting but it will be hidden from sight and only show the format you want.

        <td><span style="display:none">20130923</span>23 September 2013</td>    
    
    0 讨论(0)
  • 2021-02-02 12:50

    Easier way use:

    dateFormat:'mm/dd/yyyy hh:mm:ss'
    
    0 讨论(0)
  • 2021-02-02 12:51

    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 讨论(0)
提交回复
热议问题