使用jQuery表分类器插件对日期列进行排序时出现问题



我为我的表实现了jQuery TableSorter插件,除特定列外,所有插件都运行良好,该列包含以下格式:HH:mm, dd.MM,例如09:45, 15.1115:48, 16.11

正如我以前使用这个插件所做的那样,我在javascript文件中尝试了这个方法,该文件对表进行排序:

$(function() 
$(".my-table").tablesorter({
sortList: [[1,0]], // sorting on the second column
dateFormat : "HH:mm, dd.MM"
});
});

但是,它只按HH:mm进行排序,这会导致错误的条目(因为它忽略了日期(。有没有其他特定的日期字符串可以使用它,因为我真的无法更改它,或者有没有一种方法可以编写我自己的自定义解析器并用插件实现它?非常感谢。

dateFormat选项只允许3种设置:"mmddyyyy"(默认(、"ddmmyyyy""yyyymmdd"

在这种情况下,您需要添加一个自定义列解析器——请参阅此演示,但它只有一个Sugar.js库的示例。

特别是,请确保加载并使用此解析器,该解析器包括Sugar.js和Date.js库的接口。

如果有人偶然发现这种特定的日期格式,我就找到了一个解决方案。事实证明,TableSorter插件允许添加一个自定义解析器,可以满足您的需求。点击此处了解更多信息-https://mottie.github.io/tablesorter/docs/example-parsers.html.

在这个特定的情况下,它看起来是这样的,现在可以工作了,列是可排序的:

$.tablesorter.addParser({
id: 'custom_date_parser',
// Auto-detection
is: function(date) {
return false;
},
// Normalize data and allow sorting for this specific type
format: function(date) {
// get current year
var current_year = new Date().getFullYear()
// obtain date values from column
// current format HH:mm, dd.MM
var hour = date.slice(0, 2);
var minutes = date.slice(3, 5);
var day = date.slice(7, 9);
var month = date.slice(10, 12);
// convert to date object
return date = new Date(current_year, month - 1, day, hour - 1, minutes)
},
parsed: false,
// Type of sorting to use
type: 'numeric'
});
// Perform the sorting
$("#table-name").tablesorter({
// apply custom parser only to the second column
headers: {
1: {
sorter: 'custom_date_parser'
}
}
});

最新更新