问题描述
我正在使用datatables jquery插件,并按日期排序。我知道他们有一个插件,但我找不到从哪里实际下载它
我相信我需要这个文件:dataTables.numericComma.js,但我无法找到它,当我下载数据表,它不似乎是在zip文件。
我也不知道我是否需要使自己的自定义日期排序器进入这个插件。
我正在尝试排序格式MM / DD / YYYY HH:MM TT(AM | PM)
谢谢
修改
如何更改此按MM / DD / YYYY排序HH:MM TT(AM | PM)并将其更改为美国日期?
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));
};
点击 >日期(dd / mm / YYY),那么您可以复制并粘贴那里提供的插件代码
更新:我想你只需要切换数组的顺序,如下所示:
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
我们
更新#2:你应该能够使用日期对象进行比较。尝试这样:
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));
};
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
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.
I am trying to sort this format MM/DD/YYYY HH:MM TT(AM |PM)
Thanks
Edit
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));
};
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));
};
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.
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插件排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!