JQuery的DataTables文档很差。很抱歉也许是微不足道的问题。
我希望通过某些列上的某个字符串过滤我的表。这是我的代码:
var str = "days";
my_table.fnFilter(str, 9);
我希望只有在9列包含“days”字符串的行(从0开始计数)。但上面的代码并没有给我带来结果
我尝试将此代码用于RexEx:
var regEx = "days";
my_table.fnFilter(regEx, 9, true);
此代码:
var regEx = "^days$";
my_table.fnFilter(regEx, 9, true);
和这段代码:
var regEx = ".*days.*";
my_table.fnFilter(regEx, 9, true);
一切都没有运气。我确信我的表格在9列有“101天”字符串。我做错了什么?
更新:我的专栏有自定义类型:
jQuery.fn.dataTableExt.oSort['days-asc'] = function (a, b) {
var x = parseInt((trim(a) == "") ? 0 : trim(a.replace(/days/, "")));
var y = parseInt((trim(b) == "") ? 0 : trim(b.replace(/days/, "")));
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['days-desc'] = function (a, b) {
var x = parseInt((trim(a) == "") ? 0 : trim(a.replace(/days/, "")));
var y = parseInt((trim(b) == "") ? 0 : trim(b.replace(/days/, "")));
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
更新2: 我的初始化选项表:
$.my_table = $("#myTable").dataTable({
"iDisplayLength": 10,
"bStateSave": false,
"bFilter": true,
"bSort": true,
"bAutoWidth": false,
"oLanguage": {
"sLengthMenu": sStdMenu + " per page"
},
"sPaginationType": "full_numbers",
"sDom": 'rti<"bottom2"p><"bottom1"l',
"aaSorting": [
[0,'asc']
],
"aoColumns": [
{ "sType": "html-trimmed" },
{ "sType": "date-direct" },
{ "sType": "date-direct" },
{ "sType": "html" },
{ "sType": "percent" },
{ "sType": "money" },
{ "sType": "money" },
{ "sType": "money" },
{ "sType": "days" },
{ "sType": "days" },
{ "sType": "days" },
{ "sType": "days" },
{ "sType": "days" },
{ "sType": "days" },
{ "sType": "html" },
{ "sType": "html" },
null,
null,
null
]
});
答案 0 :(得分:0)
我没有看到您的初始化代码或fnFilter
函数的语法有任何问题。如果有帮助,这里是fnFilter
函数的完整示例,所有参数都设置为默认值(filterString
和column
除外):
$(document).ready(function () {
'use strict';
var my_table = $('#example').dataTable(),
filterDataTable = function filterDataTable() {
var filterString = 'days',
column = 9,
treatAsRegex = false,
smartFiltering = true,
showFilter = true,
caseInsensitive = true;
my_table.fnFilter(filterString, column, treatAsRegex, smartFiltering, showFilter, caseInsensitive);
};
$('#someElement').click(function (e) {
filterDataTable();
e.preventDefault();
return false;
});
});
另外,在完全不相关的注释中,您的排序功能可以简化为:
jQuery.fn.dataTableExt.oSort['days-asc'] = function (a, b) {
var x = parseInt(a, 10) || 0,
y = parseInt(b, 10) || 0;
return x - y;
};
jQuery.fn.dataTableExt.oSort['days-desc'] = function (a, b) {
var x = parseInt(a, 10) || 0,
y = parseInt(b, 10) || 0;
return y - x;
};