2016-05-11 33 views
0

私はJquery tablesorterを使用しており、ソートする必要があるdatetimeを持っています。JQuery Tablesorter sort datetime

フォーマット: - 月2016年5月2日午前12時31分00秒

現在、イムは、「時間」ソーターを使用したが、それは動作しません。

2:{ 
    sorter: 'time' 
    } 

使用できるソーターはありますか?

答えて

0

あなたは時間単位で並べ替えたいと思いますか?日付と時間で並べ替えを行う場合は、 "shortDate"パーサ(demo)を使用します。

あなたは私のfork of tablesorterを使用している場合、これは余分なコードなし(demoを)動作するはずです:

$(function() { 
    $('table').tablesorter({ 
     headers: { 
     0: { 
      sorter: 'time' 
     } 
     } 
    }); 
}); 

あなたは、元のtablesorterを使用している場合は、このパーサ(demo)が含ま

$(function() { 
    var ts = $.tablesorter, 
    dateReplace = /(\S)([AP]M)$/i, 
    // match 24 hour time & 12 hours time + am/pm - see http://regexr.com/3c3tk 
    timeTest = /^([1-9]|1[0-2]):([0-5]\d)(\s[AP]M)$|^((?:[01]\d|[2][0-4]):[0-5]\d)$/i, 
    timeMatch = /([1-9]|1[0-2]):([0-5]\d)(\s[AP]M)|((?:[01]\d|[2][0-4]):[0-5]\d)/i; 

    ts.addParser({ 
    id: 'time2', 
    is: function(str) { 
     return timeTest.test(str); 
    }, 
    format: function(str, table) { 
     // isolate time... ignore month, day and year 
     var temp, 
     timePart = (str || '').match(timeMatch), 
     orig = new Date(str), 
     // no time component? default to 00:00 by leaving it out, but only if 
     // str is defined 
     time = str && (timePart !== null ? timePart[0] : '00:00 AM'), 
     date = time ? new Date('2000/01/01 ' + time.replace(dateReplace, '$1 $2')) : time; 
     if (date instanceof Date && isFinite(date)) { 
     temp = orig instanceof Date && isFinite(orig) ? orig.getTime() : 0; 
     // if original string was a valid date, add it to the decimal so the column 
     // sorts in some kind of order; luckily new Date() ignores the decimals 
     return temp ? parseFloat(date.getTime() + '.' + orig.getTime()) : date.getTime(); 
     } 
     return str; 
    }, 
    type: 'numeric' 
    }); 

    $('table').tablesorter({ 
    headers: { 
     0: { 
     sorter: 'time2' 
     } 
    } 
    }); 
});