我正在使用一个数据表,它有 5 列( http://datatables.net/ ),
列是
- 日期格式为: Jan 5
- 时间 格式为: 10:31 AM (xx:xx XX)
- 第 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
- Date in format of: Jan 5
- Time in format of: 10:31 AM (xx:xx XX)
- 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 ???
};
发布评论
评论(3)
您可以通过将输入字符串中的时间解析为日期对象,然后比较日期对象来完成此操作:
此处的工作演示: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?
详细阐述之前的例子;即使时间单元格为空,以下代码片段也会排序。
Elaborating from earlier examples; following snippet will sort even if the time cell is empty.
这就是
使用此答案中的一段代码从 Javascript 中的用户输入解析时间为 Date 对象的最佳方法是什么? 并使用 dataTables 进行测试1.9.4,此代码需要在 dataTables.js 作为插件之后调用,然后只需将 de
sType
设置为您的列{sType: "time-sort"}.示例:
假设您的表只有一列包含时间值
This is what worked for me
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:Assuming that your table only have one column with the time values