Javascript 按UK日期对jQuery数据表排序并忽略空单元格

Javascript 按UK日期对jQuery数据表排序并忽略空单元格,javascript,jquery,sorting,date,jquery-datatables,Javascript,Jquery,Sorting,Date,Jquery Datatables,以dd/mm/yyyy格式排序日期时,如何使空单元格粘到底部?我的问题在这里(按年龄列排序): 此处的著名代码如下所示: $.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) { var retVal; x = x.replace(' ', ''); y = y.replace(' ', ''); if (x == y) retVal = 0; else

dd/mm/yyyy
格式排序日期时,如何使空单元格粘到底部?我的问题在这里(按年龄列排序):

此处的著名代码如下所示:

    $.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) {
    var retVal;
    x = x.replace(' ', '');
    y = y.replace(' ', '');

    if (x == y) retVal = 0;
    else if (x.substr(0,1) == "{" && y.substr(0,1) == "{") {
        if (x > y) retVal=  1;
        else retVal =  -1;
    }
    else if (x.substr(0,1) == "{") retVal =  1;
    else if (y.substr(0,1) == "{") retVal =  -1;

    else if (x > y) retVal=  1;
    else return -1;

    return retVal;
}
$.fn.dataTableExt.oSort['mystring-desc'] = function(y,x) {
    var retVal;
    x = x.replace(' ', '');
    y = y.replace(' ', '');

    if (x == y) retVal= 0;
    else if (x.substr(0,1) == "{" && y.substr(0,1) == "{") {
        if (x > y) retVal=  -1;
        else retVal =  1;
    }  
    else if (x.substr(0,1) == "{") retVal =  -1;
    else if (y.substr(0,1) == "{") retVal =  1;

    else if (x > y) retVal =  1;
    else return -1;

    return retVal;
 }
但它并没有解决我在dd/mm/yyy格式中对列“age”排序的问题。 它只是将我的列设置为整数格式,这不应该是因为它是日期格式。

请参见下面的代码段。基本上,您需要实现一个自定义排序功能。下面是该排序函数的代码以及说明:

//添加一组自定义排序函数
extend(jQuery.fn.dataTableExt.oSort{
“customdatesort pre”:函数(a){
//返回单元格值的“权重”
var r,x;
如果(a==null | | a===“”){
//对于空单元格:重量是一个“特殊”值,需要特殊处理
r=假;
}否则{
//否则:权重是日期的“时间值”
x=a.拆分(“/”);
r=+新日期(+x[2],+x[1]-1,+x[0]);
}
console.log(“[PRECALC]”+a+”变为“+r”);
返回r;
},
“customdatesort asc”:函数(a、b){
//返回值在Array.prototype.sort文档中解释
如果(a==false&&b==false){
//如果两个都是空单元格,则顺序无关紧要
返回0;
}else if(a==false){
/a如果是空单元,则考虑大于b
返回1;
}else if(b==false){
//如果B是空单元,则考虑小于B
返回-1;
}否则{
//常识
返回a-b;
}
},
“customdatesort desc”:函数(a、b){
如果(a==false&&b==false){
返回0;
}else if(a==false){
返回1;
}else if(b==false){
返回-1;
}否则{
返回b-a;
}
}
});
$(文档).ready(函数(){
$(“#hr#U课程#U实习生”)。数据表({
“aoColumns”:[{
“sType”:“customdatesort”
},
无效的
无效的
无效的
无效的
无效的
无效的
无效的
无效的
]
});
});

年龄
位置
-
-
-
-
-
-
-
31/12/2015
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
31/12/2014
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
14/11/2014
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
31/12/2013
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯
阿索斯

你好,谢谢@salman。。但是,如何使用类名来定位我想要排序的标题呢?谢谢^ ^您阅读了手册!数据表最近似乎改变了很多东西,所以我不确定。呵呵。谢谢你责骂我,我现在可以用了,感谢上帝保佑:DDD hi@salman你能解释一下
r=+新日期(+x[2],+x[1]-1,+x[0])中“+”的含义吗谢谢你的帮助
+
将表达式转换为数字,例如,如果
x[2]
是字符串
“2014”
,则
+x[2]
返回数字
2014
(您可以改用
parseInt
)。
    $.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) {
    var retVal;
    x = x.replace(' ', '');
    y = y.replace(' ', '');

    if (x == y) retVal = 0;
    else if (x.substr(0,1) == "{" && y.substr(0,1) == "{") {
        if (x > y) retVal=  1;
        else retVal =  -1;
    }
    else if (x.substr(0,1) == "{") retVal =  1;
    else if (y.substr(0,1) == "{") retVal =  -1;

    else if (x > y) retVal=  1;
    else return -1;

    return retVal;
}
$.fn.dataTableExt.oSort['mystring-desc'] = function(y,x) {
    var retVal;
    x = x.replace(' ', '');
    y = y.replace(' ', '');

    if (x == y) retVal= 0;
    else if (x.substr(0,1) == "{" && y.substr(0,1) == "{") {
        if (x > y) retVal=  -1;
        else retVal =  1;
    }  
    else if (x.substr(0,1) == "{") retVal =  -1;
    else if (y.substr(0,1) == "{") retVal =  1;

    else if (x > y) retVal =  1;
    else return -1;

    return retVal;
 }