1
這是我的JS。排序是時間在hh:mm格式。當我運行它時,我得到的錯誤:oSort [(sDataType?sDataType:「string」)+「 - 」+ aaSort [i] [1]]不是函數我想排序的默認列我聲明瞭這一點。排序前兩列或最後一列的作品,但它不喜歡當我嘗試默認任何其他列。看來DataTable很難弄清楚在初始化期間sDataType是用於那些列的。aaSorting無法識別使用aColumns定義的自定義排序
<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));
};