如何使用格式为'dd/mm/yyyy-dd/mm/yyyy'的jquery tablesorter对日期进行排序 [英] How to sort date with jquery tablesorter which is in format 'dd/mm/yyyy - dd/mm/yyyy'

查看:110
本文介绍了如何使用格式为'dd/mm/yyyy-dd/mm/yyyy'的jquery tablesorter对日期进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期是dd/mm/yyyy-dd/mm/yyyy格式.如何使用jQuery tablesorter对此进行排序.我尝试使用sorter:"shortDate"和dateFormat:'ddmmyyyy'对它进行排序,但是它没有以正确的顺序对其进行排序.

I am having date in dd/mm/yyyy - dd/mm/yyyy format. How to sort this with jquery tablesorter. I try to sort it with sorter:"shortDate" and dateFormat:'ddmmyyyy' but it doesn't sort it in correct order.

<table>
<thead> 
    <tr><th>Date</th></tr>
</thead> 
<tbody>
<tr class="reportcell  odd ">
    <td>
        <div class="time">28/04/2014 - 11/07/2014</div>
    </td>
</tr>
<tr>
    <td>
        <div class="time">28/04/2014 - 13/05/2014</div>
    </td>
</tr>
<tr>
    <td>
        <div class="time">22/07/2014 - 22/07/2014</div>
    </td>
</tr>
<tr>
    <td>
        <div class="time">22/05/2014 - 22/05/2014</div>
    </td>
</tr>
</tbody>

推荐答案

您需要创建一个自定义解析器才能对列进行排序.该解析器存在问题,因为很难使其与过滤器小部件一起正常工作( demo ):

You'll need to make a custom parser to be able to sort a column. There is an issue with this parser as it would be difficult to make it work properly with the filter widget (demo):

$(function() {

    $.tablesorter.addParser({
        id: "date-range",
        is: function(){
            return false;
        },
        format: function(s, table, cell) {
            var dates = s.replace(/(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{4})/g, "$2/$1/$3").split(' - '),
                parsed = [];
            if (dates.length) {
                $.each(dates, function(i,d){
                    var v = new Date(d);
                    parsed.push($.type(v) === 'date' ? v.getTime() : d);
                });
            }
            return parsed.length ? parsed.join('') : s;
        },
        parsed : true,
        type: "text"
    });

    // call the tablesorter plugin
    $("table").tablesorter({
        headers : {
            0 : { sorter: 'date-range' }
        }
    });

});


更新(评论中的问题):


Update (question from comments):

格式功能代码执行以下操作:

The format function code does the following:

  • 通过将日期从"dd/mm/yyyy"更改为"mm/dd/yyyy"来修改日期,以便日期解析器(new Date()将识别所需的日期设置)
  • split(' - ')拆分日期范围,并从范围字符串中在数组中创建两个日期.
  • dates.length确保我们有一个包含内容的数组
  • $.each()在每个日期字符串中循环
    • 它从字符串创建数据对象
    • 然后将其作为日期(以毫秒为单位)或原始字符串(如果不是有效日期)添加到新数组中
    • Modifiy the date by changing it from "dd/mm/yyyy" to "mm/dd/yyyy" so the date parser (new Date() will recognize the desired date setting)
    • The split(' - ') breaks apart the date range and creates two dates in an array from the range string.
    • dates.length makes sure we have an array with content
    • $.each() cycles through each date string
      • It creates a data object from the string
      • Then adds it to a new array as either a date in milliseconds or the original string if it isn't a valid date

      因此,该方法无法与过滤器小部件一起使用的原因是因为日期被合并(作为字符串,未添加)以允许对解析的日期进行正确的排序.同样,从写出此描述后,我意识到具有单个日期的单元格不会被解析为毫秒时间,而是作为原始日期字符串返回.无论哪种方式,非日期范围单元格都会与日期范围单元格分开排序.

      So, the reason why this method won't work with the filter widget is because the dates are combined (as a string, not added) to allow proper sorting of the parsed date. Also, from writing this description out I realize that a cell with a single date will not get parsed into a time in milliseconds, but instead be returned as the original date string. Either way non-date range cells will sort separate from the date range cells.

      这篇关于如何使用格式为'dd/mm/yyyy-dd/mm/yyyy'的jquery tablesorter对日期进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆