aaSorting无法识别使用aoColumns定义的自定义排序 [英] aaSorting fails to recognize a custom sort defined using aoColumns

查看:411
本文介绍了aaSorting无法识别使用aoColumns定义的自定义排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的JS。时间是以hh:mm格式表示的。当我运行它时,出现错误: oSort [(sDataType?sDataType: string)+- + aaSort [i] [1]]不是函数,我正在尝试排序我声明其排序的列的默认值。在前两列或最后一列上进行排序是可行的,但是当我尝试默认为其他任何列时,它不一样。似乎DataTable很难确定初始化期间这些列的sDataType是什么。

This is my JS. The sort is for time in the hh:mm a format. When I run it I get the error: oSort[(sDataType ? sDataType : "string") + "-" + aaSort[i][1]] is not a function I am trying to sort on default for a column for which I declare the sort. Sorting on the first two or last columns works but it does not like when I try to default to any of the other columns. It seems that the DataTable is having a hard time figuring out what the sDataType is for those columns during initialization.

<script type="text/javascript">

$(document).ready(function() {
    $('#example').dataTable({

        "bJQueryUI": true,
        "iDisplayLength": 50,
        "aoColumns": [
            null,
            null,
            { "sType": 'string-case' },
            { "sType": 'string-case' },
            { "sType": 'string-case' },
            { "sType": 'string-case' },
            null
        ],
        "aaSorting": [[ 2, 'desc']]
     } );
} );

function getTimeValue(x) {
    var time = x.match(/(\d+)(?::(\d\d))?\s*(P?)/);
    var h = parseInt(time[1],10) + (time[3] ? 12 : 0);
    if(!time[3] && parseInt(time[1],10)==12) h = 0;
    if(time[3] && parseInt(time[1],10)==12) h = 12;
    return h * 60 + ( parseInt(time[2],10) || 0 );
}

/* Define two custom functions (asc and desc) for string sorting */
jQuery.fn.dataTableExt.oSort['string-case-asc']  = function(x,y) {

    x = getTimeValue(x);
    y = getTimeValue(y);

    return ((x < y) ? -1 : ((x > y) ?  1 : 0));

};

jQuery.fn.dataTableExt.oSort['string-case-desc'] = function(x,y) {

    x = getTimeValue(x);
    y = getTimeValue(y);

    return ((x < y) ?  1 : ((x > y) ? -1 : 0));
};

推荐答案

几乎不好意思发布此内容,但如果它可以帮助其他人,那将是值得的。简单的解释:getTimeValue和jQuery.fn需要在调用DataTable之前进行初始化,所以我要做的就是将这三个函数移到$(document).ready(function()之前。在Objective-C中也是常见的编程错误。

Almost embarrassed to post this but if it helps anyone else it'll be worth it. Simple explanation: the getTimeValue and jQuery.fn need to be initialized beofre the call to setup the DataTable so all I needed to do was move those three functions before the $(document).ready(function() . Im new to JS but this is a common programming error in objective-c also. bleh.

这篇关于aaSorting无法识别使用aoColumns定义的自定义排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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