我为我的表实现了jQuery TableSorter附加组件,并且所有工作正常,但该特定列包含以下格式:HH:mm, dd.MM
,例如09:45, 15.11
或15:48, 16.11
。
就像我之前使用此插件所做的那样,我尝试了在javascript文件中对表进行排序:
$(function()
$(".my-table").tablesorter({
sortList: [[1,0]], // sorting on the second column
dateFormat : "HH:mm, dd.MM"
});
});
但是,它仅按进行排序HH:mm
,这会导致输入错误(因为它会忽略日期)。是否有另一个特定的日期字符串可以使用,因为我无法真正更改它,或者有没有办法编写自己的自定义解析器并使用插件来实现?谢谢!
如果有人偶然发现了这种特定的日期格式,我就找到了解决方案。事实证明,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'
}
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句