如何在 javascript 中按时间排序(格式:下午 5:40)以便与 DataTables 一起使用?

发布于 2024-12-25 07:40:33 字数 1459 浏览 0 评论 0 原文

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

列是

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

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

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

对于日期,这已经完美运行。数据表中最多只有 2 天的数据,因此即使滚动到该月的第一天,它仍然会显示在顶部,这很好。我查看了文档,但对如何对“时间”列进行正确排序感到困惑。

这是我的代码:

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 属性构建某种自定义的时间排序算法(你可以看到它在上面的示例链接中,他区分大小写进行排序),我对如何执行此操作一无所知:(我什至不确定到目前为止我是否正确执行了此操作。似乎对两列进行了很好的排序,但现在我需要做到这一点,以便时间合适......

这是我认为我需要的代码的另一部分(这再次来自示例)。在我的自定义时间升序和排序代码中下降。

/* 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 ???
};

I have a datatable I'm using which has 5 columns ( http://datatables.net/ )

The columns are

  1. Date in format of: Jan 5
  2. Time in format of: 10:31 AM (xx:xx XX)
  3. Columns 3, 4, 5 aren't important, they're just data that I dont care about the sorting as long as 1 & 2 are correct.

I want to sort by Date FIRST (most recent), then I want to sort by Time (most recent at top).

So Jan 5, 4:58 PM should show before 4:58 AM, and obviously all the other numbers need to work as well for all other times. The format is always the same, ie: 12:34 AM, 4:15 PM, 12:00 AM, etc.

For the date, that already works perfectly. There's only 2 days of data max in the datatable, so even when it rolls over to the 1st of the month, that will still show at the top which is fine. I have looked at the documentation and I'm confused how to do the correct sorting for my Time column.

Here is my code:

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

});

This is from here: http://datatables.net/release-datatables/examples/basic_init/multi_col_sort.html

I take it now I have to build some sort of custom sorting algorithm for time using the sType property for "aoColumns" (you can see it in the example link above where he sorts case sensitively), and I have zero idea how to do this :( I'm not quite even sure if I did this right so far. It seems to sort the two columns fine, but now I need to make it so time is proper...

Here is the other part of the code that I believe I need. (once again, this is from the example). I'm 99% sure this is where I need to put in my custom time sorting code for both ascending and decending.

/* 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 ???
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野心澎湃 2025-01-01 07:40:33

您可以通过将输入字符串中的时间解析为日期对象,然后比较日期对象来完成此操作:

此处的工作演示:http://live.datatables.net/erefom/2/edit#preview

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

另请参阅此答案:将用户的时间解析为 Date 对象的最佳方法是什么输入JavaScript?

You can do this by parsing the time from your input string into a date object and then comparing the date objects:

Working Demo here: http://live.datatables.net/erefom/2/edit#preview

Source here: http://live.datatables.net/erefom/2/edit

Also see this answer: What is the best way to parse a time into a Date object from user input in Javascript?

掐死时间 2025-01-01 07:40:33

详细阐述之前的例子;即使时间单元格为空,以下代码片段也会排序。

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

Elaborating from earlier examples; following snippet will sort even if the time cell is empty.

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;
  }
}
黑色毁心梦 2025-01-01 07:40:33

这就是

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

使用此答案中的一段代码从 Javascript 中的用户输入解析时间为 Date 对象的最佳方法是什么? 并使用 dataTables 进行测试1.9.4,此代码需要在 dataTables.js 作为插件之后调用,然后只需将 de sType 设置为您的列 {sType: "time-sort"}.示例:

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

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

This is what worked for me

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

using a piece of code from this answer What is the best way to parse a time into a Date object from user input in Javascript? and tested with dataTables 1.9.4, this code need to be called after the dataTables.js as a plugin, then just need to set de sType to your column {sType: "time-sort"}. Example:

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

Assuming that your table only have one column with the time values

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文