【问题标题】:How do I sort by time (format: 5:40 PM) in javascript for use with DataTables?如何在 JavaScript 中按时间(格式:下午 5:40)排序以用于 DataTables?
【发布时间】:2012-01-06 01:06:57
【问题描述】:

我有一个我正在使用的数据表,它有 5 列 (http://datatables.net/)

列是

  1. 日期格式为:1 月 5 日
  2. 时间格式为:上午 10:31 (xx:xx XX)
  3. 第 3、4、5 列并不重要,它们只是我不关心排序的数据,只要 1 和 2 正确即可。

我想先按日期排序(最近的),然后我想按时间排序(最近的在顶部)。

因此,1 月 5 日下午 4:58 应该在凌晨 4:58 之前显示,显然所有其他数字在所有其他时间都需要正常工作。格式始终相同,即:12:34 AM、4:15 PM、12:00 AM 等。

对于日期,这已经很完美了。数据表中只有 2 天的数据最大值,所以即使它滚动到本月 1 日,它仍然会显示在顶部,这很好。我查看了文档,但我很困惑如何对我的时间列进行正确排序。

这是我的代码:

oTable = $('#posts').dataTable({
    "bSort": true,
    "aaSorting": [ [0,'desc'], [1,'asc'] ],
    "aoColumns": [
                null,
                { "sType": 'time-sort' },
                null,
                null,
                null
           ]

});

来自这里:http://datatables.net/release-datatables/examples/basic_init/multi_col_sort.html

我现在接受它,我必须使用“aoColumns”的 sType 属性为时间构建某种自定义排序算法(您可以在上面的示例链接中看到它,他区分大小写的地方),我不知道如何要做到这一点:(我什至不确定到目前为止我是否做得对。似乎对两列进行了很好的排序,但现在我需要做到这一点,以便时间合适......

这是我认为我需要的代码的另一部分。 (再一次,这是来自示例)。我 99% 确定这是我需要为升序和降序输入自定义时间排序代码的地方。

/* Define two custom functions (asc and desc) for time sorting */
jQuery.fn.dataTableExt.oSort['time-sort-asc']  = function(x,y) {
    return ???;
};

jQuery.fn.dataTableExt.oSort['time-sort-desc'] = function(x,y) {
    return ???
};

【问题讨论】:

  • 所以“2 月 28 日”高于“3 月 1 日”但“3 月 31 日”低于“4 月 1 日”(即倒转日历顺序)?
  • 最多只有 2 天的数据,.. 但它需要显示最新到最旧的数据。所以 4 月 1 日在顶部,然后是 3 月 31 日、3 月 1 日、2 月 28 日(如果它显示了那么多数据,那就是)
  • 现在我想了想,我认为日期可能也需要自定义排序,但这不如时间重要。我很快就能弄清楚。 1号以上按降序排列,可以在上面也可以在下面,这是不对的。
  • 是的,我认为它会根据日期进行适当的排序。但它只有一个月中的 1 天我需要担心,所以时间将是必不可少的东西,然后我会弄清楚日期

标签: javascript jquery sorting time datatables


【解决方案1】:

您可以通过将输入字符串中的时间解析为日期对象,然后比较日期对象来做到这一点:

工作演示在这里:http://live.datatables.net/erefom/2/edit#preview

来源:http://live.datatables.net/erefom/2/edit

另请参阅此答案:What is the best way to parse a time into a Date object from user input in Javascript?

【讨论】:

【解决方案2】:

从前面的例子中详细说明;即使时间单元格为空,以下 sn-p 也会排序。

function getTimeValue(x) {
  // if the cell is not empty then parse it, otherwise just return 0 as the value; so it will be sorted appropriately.      
  if (x != '') {
    var time = x.match(/(\d+)(?::(\d\d))?\s*(P?)/);  
    var h = parseInt(time[1]) + (time[3] ? 12 : 0);
    if(!time[3] && parseInt(time[1])==12) h = 0;
    if(time[3] && parseInt(time[1])==12) h = 12;

    return h * 60 + ( parseInt(time[2]) || 0 );
  } else {
    return 0;
  }
}

【讨论】:

    【解决方案3】:

    这对我有用

    jQuery.extend(jQuery.fn.dataTableExt.oSort, {
        "time-sort-pre": function(a) {
            if (a == '')
                return null;
    
            var time = a.match(/(\d+)(:(\d\d))?\s*(p?)/i);
            if (time == null)
                return null;
    
            var hours = parseInt(time[1], 10);
            if (hours == 12 && !time[4]) {
                hours = 0;
            }
            else {
                hours += (hours < 12 && time[4]) ? 12 : 0;
            }
    
            var d = new Date();
            d.setHours(hours);
            d.setMinutes(parseInt(time[3], 10) || 0);
            d.setSeconds(0, 0);
            return d;
        },
        "time-sort-asc": function(a, b) {
            return ((a < b) ? -1 : ((a > b) ? 1 : 0));
        },
        "time-sort-desc": function(a, b) {
            return ((a < b) ? 1 : ((a > b) ? -1 : 0));
        }
    });
    

    使用此答案What is the best way to parse a time into a Date object from user input in Javascript? 中的一段代码并使用dataTables 1.9.4 进行测试,此代码需要在dataTables.js 作为插件之后调用,然后只需将de sType 设置为您的列@ 987654324@。示例:

    $('#datatable-list').dataTable(
        aoColumns: [{sType: "time-sort"}]
    );
    

    假设您的表格只有一列包含时间值

    【讨论】:

      猜你喜欢
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 2021-11-09
      • 1970-01-01
      • 2017-12-22
      • 2021-09-17
      • 1970-01-01
      相关资源
      最近更新 更多