在 fnDrawCallback 之后更新 DataTables 过滤/排序索引
我正在使用 timeago jquery 插件 来格式化我的 数据表。 fnDrawCallback
DataTables 选项允许我设置/更新格式化时间戳,例如:
<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>
转换为更易读的2 年前
。只要有表格重绘,就会调用此函数,因此它适用于分页、服务器调用等。
myTable = $('#example').dataTable({
"fnDrawCallback" : function () { $("abbr.timeago").timeago() },
"aaData" : tableData,
"aoColumns" : tableHeadings,
"oSearch" : {"sSearch" : "", "bRegex" : true, "bSmart" : true, },
});
但是,fnDrawCallback
不会更新搜索索引,因此查询years
不会返回任何内容。有没有办法强制 DataTables 更新其搜索索引?
编辑
为了清楚起见,我初始化了 tableData
和 tableHeadings
通过 JSON 传递的哈希数组。这似乎工作正常,因为这些数据顺利地显示在表中。问题是 Timeago 修改了 fnDrawCallback
上的 DOM,这是在填充表的过滤/排序索引之后发生的。
// tableHeadings === ['a', 'b', 'DateOne', 'DateTwo', ...]
for(i=0; i<tableHeadings.length; i++) {
tableHeadings[i] = { "sTitle" : tableHeadings[i], };
if(tableHeadings[i].sTitle.match(/Date/)) {
tableHeadings[i].fnRender = function (c) { // closure over i
return function (o) {
var iso_time = o.aData[c].replace(/\s/, "T") + "Z";
return '<abbr class="timeago" title="'+iso_time+'">'+'</abbr>';
}
}(i);
}
}
因此我可以搜索 iso_time
,但不能搜索 2 年前
。
I'm using the timeago jquery plugin to format timestamps in columns of my data table. The fnDrawCallback
DataTables option allows me to set / update formatted timestamps like:
<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>
into a more human readable 2 years ago
. This gets called anytime there is a table redraw so it works for pagination, server calls, etc.
myTable = $('#example').dataTable({
"fnDrawCallback" : function () { $("abbr.timeago").timeago() },
"aaData" : tableData,
"aoColumns" : tableHeadings,
"oSearch" : {"sSearch" : "", "bRegex" : true, "bSmart" : true, },
});
However, fnDrawCallback
does not update the search indicies so querying years
returns nothing. Is there a way to force DataTables to update its search indicies?
Edit
For clarity I initialize tableData
and tableHeadings
an array of hashes passed in through JSON. This seems to work fine, as this data shows up in the table without a hitch. The issue is that the Timeago modifies the DOM on the fnDrawCallback
which happens after the filter / sort indicies for the table have been populated.
// tableHeadings === ['a', 'b', 'DateOne', 'DateTwo', ...]
for(i=0; i<tableHeadings.length; i++) {
tableHeadings[i] = { "sTitle" : tableHeadings[i], };
if(tableHeadings[i].sTitle.match(/Date/)) {
tableHeadings[i].fnRender = function (c) { // closure over i
return function (o) {
var iso_time = o.aData[c].replace(/\s/, "T") + "Z";
return '<abbr class="timeago" title="'+iso_time+'">'+'</abbr>';
}
}(i);
}
}
So I can search for the iso_time
, but not 2 years ago
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论