如何使用 DataTables jquery 插件按日期排序? [英] How to sort by Date with DataTables jquery plugin?
问题描述
我正在使用数据表 jquery 插件并希望按日期排序.
I am using the datatables jquery plugin and want to sorty by dates.
我知道他们有一个插件,但我找不到从哪里下载它
I know they got a plugin but I can't find where to actually download it from
http://datatables.net/plug-ins/sorting
我相信我需要这个文件:dataTables.numericComma.js 但我在任何地方都找不到它,而且当我下载数据表时,它似乎不在 zip 文件中.
I believe I need this file: dataTables.numericComma.js yet I can't find it anywhere and when I download datatables it does not seem to be in the zip file.
我也不确定是否需要制作我自己的自定义日期排序器来传递给这个插件.
I am also not sure if I need to make my own custom date sorter to pass into this plugin.
我正在尝试对这种格式进行排序 MM/DD/YYYY HH:MM TT(AM |PM)
I am trying to sort this format MM/DD/YYYY HH:MM TT(AM |PM)
谢谢
编辑
如何将其更改为按 MM/DD/YYYY HH:MM TT(AM |PM) 排序并将其更改为美国日期?
How can I change this to sort by MM/DD/YYYY HH:MM TT(AM |PM) and change it to U.S date?
jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function(a,b) {
var ukDatea = a.split('/');
var ukDateb = b.split('/');
var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
var ukDatea = a.split('/');
var ukDateb = b.split('/');
var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
推荐答案
点击Date (dd/mm/YYY)下的show details"链接,然后你就可以复制粘贴那个插件了那里提供的代码
Click on the "show details" link under Date (dd/mm/YYY), then you can copy and paste that plugin code provided there
更新:我认为您可以切换数组的顺序,如下所示:
Update: I think you can just switch the order of the array, like so:
jQuery.fn.dataTableExt.oSort['us_date-asc'] = function(a,b) {
var usDatea = a.split('/');
var usDateb = b.split('/');
var x = (usDatea[2] + usDatea[0] + usDatea[1]) * 1;
var y = (usDateb[2] + usDateb[0] + usDateb[1]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['us_date-desc'] = function(a,b) {
var usDatea = a.split('/');
var usDateb = b.split('/');
var x = (usDatea[2] + usDatea[0] + usDatea[1]) * 1;
var y = (usDateb[2] + usDateb[0] + usDateb[1]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
我所做的只是切换 __date_[1]
(日)和 __date_[0]
(月),并将 uk
替换为我们
所以你不会感到困惑.我认为这应该为您处理.
All I did was switch the __date_[1]
(day) and __date_[0]
(month), and replaced uk
with us
so you won't get confused. I think that should take care of it for you.
更新#2:您应该能够仅使用日期对象进行比较.试试这个:
Update #2: You should be able to just use the date object for comparison. Try this:
jQuery.fn.dataTableExt.oSort['us_date-asc'] = function(a,b) {
var x = new Date(a),
y = new Date(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['us_date-desc'] = function(a,b) {
var x = new Date(a),
y = new Date(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
这篇关于如何使用 DataTables jquery 插件按日期排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!