Sae*_*aeX 2 datatables momentjs
我正在使用数据表 1.10.5。当我尝试使用推荐的moment.js(根据http://datatables.net/blog/2014-12-18)对日期进行排序时,认为工作正常:
http://jsfiddle.net/9gohzd9t/1/
但是,当我a href向该日期添加链接 ( ) 时,它会根据链接而不是日期进行排序:
http://jsfiddle.net/dnsL2oc4/1/
关于如何在没有太多黑客攻击的情况下正确解决此问题的任何想法?
问题在于 datetime-moment.js 的 unshift 方法。Moment尝试转换<a href="12.html">12-01-2001</a>为给定的“DD-MM-YYYY”格式的有效日期,这显然不能。因此,您必须从日期中删除 html,可能使用如下函数:
function strip(html) {
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
Run Code Online (Sandbox Code Playgroud)
然后在 unshift 方法中去除字符串(用下面的代码替换 datetime-moment.js):
$.fn.dataTable.moment = function (format, locale) {
var types = $.fn.dataTable.ext.type;
// Add type detection
types.detect.unshift(function (d) {
return moment(strip(d), format, locale, true).isValid() ?
'moment-' + format :
null;
});
// Add sorting method - use an integer for the sorting
types.order['moment-' + format + '-pre'] = function (d) {
return moment(strip(d), format, locale, true).unix();
};
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7645 次 |
| 最近记录: |