如何按时间排序(格式:5:40 PM)在javascript中与DataTables一起使用? [英] How do I sort by time (format: 5:40 PM) in javascript for use with DataTables?

查看:96
本文介绍了如何按时间排序(格式:5:40 PM)在javascript中与DataTables一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



列是


  1. 日期格式为:1月5日

  2. 格式为:10:31 AM(xx:xx XX)

  3. 列3,4,5不重要,它们只是数据我不关心排序,只要1& 2是正确的。

我想按日期FIRST(最新)排序,然后我要按时间排序(最上方) / p>

所以Jan 5,4:58 PM应该在4:58 AM之前显示,显然所有其他数字在其他时间都需要工作。格式总是相同的,即:12:34 AM,4:15 PM,12:00 AM等。



对于日期,这已经很好用了。在datatable中最多只能有2天的数据,所以即使当它滚动到月的1st时,仍然会显示在顶部,这是很好的。我看过文档,我很困惑如何对我的时间列执行正确的排序。



这是我的代码:

  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%肯定这是我需要放在我的自定义时间

  / *定义两个自定义函数(asc和desc)进行时间排序* / 
jQuery.fn.dataTableExt.oSort ['time-sort- asc'] = function(x,y){
return ???;
};

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


解决方案

您可以通过从输入中解析时间将字符串转换成日期对象,然后比较日期对象:



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



来源: a href =http://live.datatables.net/erefom/2/edit =nofollow> http://live.datatables.net/erefom/2/edit



另请参阅这个答案:从JavaScript中的用户输入中将Date转换为Date对象的最佳方式是什么?


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

解决方案

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?

这篇关于如何按时间排序(格式:5:40 PM)在javascript中与DataTables一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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