tableSorter自定义日期解析器不起作用

时间:2015-06-03 08:02:53

标签: javascript sorting view tablesorter

我有以下表格分拣机配置:

 $(function () {
     $.tablesorter.addParser({
         id: "customDate",
         is: function(s) {
             //return false;
             //use the above line if you don't want table sorter to auto detected this parser
             //else use the below line.
             //attention: doesn't check for invalid stuff
             //2009-77-77 77:77:77.0 would also be matched
             //if that doesn't suit you alter the regex to be more restrictive
             return /\d{1,2}\.\d{1,2}\.\d{1,4} \d{1,2}:\d{1,2}:\d{1,2}/.test(s);
         },
         format: function(s) {
             s = s.replace(/\-/g," ");
             s = s.replace(/:/g," ");
             s = s.replace(/\./g," ");
             s = s.split(" ");
             return $.tablesorter.formatFloat(new Date(s[0], s[1]-1, s[2], s[3], s[4], s[5]).getTime());
         },
         type: "numeric"
     });
    var $table = $('#table')
        .tablesorter({
            headers: {
                        1: { sorter:'customDate' }
                    },
            sortList: [[1,1]],   // sorting(desc) by column with index 1
            dateFormat: 'dd/MM/yyyy HH:mm:ss',
            theme: 'blue',
            widthFixed: true,
            headerTemplate: '{content} {icon}',
            widgets: ['zebra', 'filter'],
            widgetOptions: {
                zebra: ["even", "odd"],
                // filter_anyMatch replaced! Instead use the filter_external option
                // Set to use a jQuery selector (or jQuery object) pointing to the
                // external filter (column specific or any match)
                filter_external: '.search',
                // add a default type search to the first name column
                filter_defaultFilter: {1: '~{query}'},
                // include column filters
                filter_columnFilters: true,
                filter_placeholder: {search: 'Искать...'},
                filter_saveFilters: true,
                filter_reset: '.reset'
            }
        })
        // needed for pager plugin to know when to calculate filtered rows/pages
        .addClass('hasFilters')
        .tablesorterPager({
            container: $(".table-pager"),
            output: '{page} из {filteredPages} ({filteredRows})',
            size: 5
        });
});

但在我运行应用程序后,我看到下表列:
enter image description here

显然该列排序错误。

为什么呢?

2 个答案:

答案 0 :(得分:1)

实际上不需要自定义日期解析器。 dateFormat option应设置为ddmmyyyy。该选项可用的唯一设置是:

  • mmddyyyy
  • ddmmyyyy
  • yyyymmdd

设置后,时间将包含在解析日期(demo)中。

答案 1 :(得分:0)

问题是我以错误的顺序传递了日期参数。

当前变体工作正常:

format: function(s) {
                 s = s.replace(/\-/g," ");
                 s = s.replace(/:/g," ");
                 s = s.replace(/\./g," ");
                 s = s.split(" ");
                 return $.tablesorter.formatFloat(new Date(s[2], s[1], s[0], s[3], s[4], s[5]).getTime());
             },