7

The organization I support dictates that all dates will be shown in dd-MON-yyyy format (10-SEP-2006, 08-MAY-2013). Please see http://jsfiddle.net/jhfrench/zpWWa/ for an example dataset.

When run on Chrome, dataTables correctly recognizes this pattern as a date.

When run on IE7, dataTables (or IE?) does not recognize this pattern as a date. Unfortunately, we have to support IE7. Is there a way to shim the "dd-MON-yyyy" format for IE, but not for Chrome or other browsers that natively support that format?

I am using IE conditions to specify the HTML tag, so I could key off of <HTML class="lt-ie9">; I'm also using Modernizr on this page (if there's a relevant test).

4

2 回答 2

4

与其尝试填充 IE7,我认为最简单的解决方案是编写自己的排序函数,然后为使用日期格式的所有列调用它。使用 DataTables 很容易做到这一点,如下所述:

http://www.datatables.net/plug-ins/sorting

您可以手动定义表格以对您的列使用新的排序算法,但这有点笨拙,因为您需要在使用该格式的任何地方执行此操作。相反,您可以自动检测,如下所述:

http://www.datatables.net/plug-ins/type-detection

我在这里用一个快速的解决方案创建了你的小提琴的一个分支——我只是将日期转换为一个简单的数字——只要你相信数据,这应该没问题。否则,您可能希望转换为实际的日期对象,这可能是一种更可靠的方法。

http://jsfiddle.net/pFdyK/

代码:

// Put these somewhere better than a global :)
var months = "JAN_FEB_MAR_APR_MAY_JUN_JUL_AUG_SEP_OCT_NOV_DEC".split("_");
function monthToOrd(month) {
    return $.inArray(month, months);
}
function customDateToOrd(date) {
    // Convert to a number YYYYMMDD which we can use to order
    var dateParts = date.split(/-/);
    var day = dateParts[0];
    var month = monthToOrd(dateParts[1]);
    var year = dateParts[2];
    var numericDate = (year * 10000) + (month * 100) + day;
    return numericDate;
}

// This will help DataTables magic detect your custom format
// Unshift so that it's the first data type (overridding in built ones)
jQuery.fn.dataTableExt.aTypes.unshift(
    function ( sData )
    {
        // You might want to make this check a little tighter so you don't match
        // invalid dates, but this should do for now
        if (sData.match(/\d{2}-[A-Za-z]{3}-\d{4}/))
            return 'custom-date';
        else
            return null;
    }
);

// define the sorts
jQuery.fn.dataTableExt.oSort['custom-date-asc']  = function(a,b) {
    var ordA = customDateToOrd(a);
    var ordB = customDateToOrd(b);
    return (ordA < ordB) ? -1 : ((ordA > ordB) ? 1 : 0);
};

jQuery.fn.dataTableExt.oSort['custom-date-desc'] = function(a,b) {
    var ordA = customDateToOrd(a);
    var ordB = customDateToOrd(b);
    return (ordA < ordB) ? 1 : ((ordA > ordB) ? -1 : 0);
};

$(document).ready(function() {
    $('html').addClass('lt-ie9');
   $('table.datatable').dataTable();
} );
于 2013-05-13T20:51:04.430 回答
0

扩展Shaun 的好答案,至少有两种方法可以测试dd-MMM-yyyy 格式的填充程序的需要。在与JSLint进行激烈协商后,我修改了 Shaun 的代码(我发誓它讨厌我写的每一行 ECMA)。

使用 IE 条件

如果您已经在使用 IE 条件 ( <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->),那么您可以测试HTML.lt-ie9并有条件地定义新的排序算法,然后调用 dataTables:

//test for html.lt-ie9
if ($('html.lt-ie9').length) {
    //create the new magic sorting
    var customDateDDMMMYYYYToOrd = function (date) {
        "use strict"; //let's avoid tom-foolery in this function
        // Convert to a number YYYYMMDD which we can use to order
        var dateParts = date.split(/-/);
        return (dateParts[2] * 10000) + ($.inArray(dateParts[1].toUpperCase(), ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]) * 100) + dateParts[0];
    };

    // This will help DataTables magic detect the "dd-MMM-yyyy" format; Unshift so that it's the first data type (so it takes priority over existing)
    jQuery.fn.dataTableExt.aTypes.unshift(
        function (sData) {
            "use strict"; //let's avoid tom-foolery in this function
            if (/^([0-2]?\d|3[0-1])-(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)-\d{4}/i.test(sData)) {
                return 'custom-date-dd-mmm-yyyy';
            }
            return null;
        }
    );

    // define the sorts
    jQuery.fn.dataTableExt.oSort['custom-date-dd-mmm-yyyy-asc'] = function (a, b) {
        "use strict"; //let's avoid tom-foolery in this function
        var ordA = customDateDDMMMYYYYToOrd(a),
            ordB = customDateDDMMMYYYYToOrd(b);
        return (ordA < ordB) ? -1 : ((ordA > ordB) ? 1 : 0);
    };

    jQuery.fn.dataTableExt.oSort['custom-date-dd-mmm-yyyy-desc'] = function (a, b) {
        "use strict"; //let's avoid tom-foolery in this function
        var ordA = customDateDDMMMYYYYToOrd(a),
            ordB = customDateDDMMMYYYYToOrd(b);
        return (ordA < ordB) ? 1 : ((ordA > ordB) ? -1 : 0);
    };
};

//now call the dataTable plugin against the target tables (in this case, any table with `class="dataTable"`)
$('table.datatable').dataTable();

请参阅http://jsfiddle.net/jhfrench/nEsCt/ 上的 IE 条件示例

使用Modernizr进行测试

另一方面,如果您倾向于使用 Modernizr 来测试功能,我们可以定义 Modernizr 测试,然后使用 Modernizr 执行测试并有条件地加载 shim 魔法(来自 .js 文件),然后调用 dataTables :

//define the Modernizr test
Modernizr.addTest('valid_date_dd_mmm_yyyy', function() {
    return !isNaN(Date.parse("17-MAY-2013"));
});

//if Modernizr determines "dd-mmm-yyyy" dates are not supported, load the following JavaScript resources
Modernizr.load([
    {
        test: Modernizr.valid_date_dd_mmm_yyyy,
        nope: 'http://appliedinter.net/Workstream/common_files/js/dataTable_shim_dd-MMM-yyyy.js',
        complete: function () {
            $(document).ready(function () {
                //now call the dataTable plugin against the target tables (in this case, any table with `class="dataTable"`)
                $('table.datatable').dataTable();
            });
        }
    }
]);

请参阅http://jsfiddle.net/jhfrench/tNkGC/ 上的 Modernizr 方法

于 2013-05-17T17:34:03.050 回答