搜索所有列
搜索表格时可以使用正则表达式。
例如,下面的代码显示的搜索结果在所有列中都包含单词 Angelica 或 London。
var table = $('#example').DataTable();
table
.search('Angelica|London', true, false)
.draw();
有关代码和演示,请参阅this jsFiddle。
搜索特定列
要搜索特定列,您可能需要使用custom search 功能。
下面的代码显示了在索引为0、1 和2 的表数据值中包含单词Angelica 或Tokyo 的搜索结果。
var table = $('#example').DataTable();
var terms = ['Angelica', 'Tokyo'];
// Convert terms to lower case
$.each(terms, function(index, value){
terms[index] = value.toLowerCase();
});
// Enable custom search
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
var isFound = false;
$.each(data, function (index, value){
// Evaluate only first three data values
if(index === 0 || index === 1 || index === 2){
// Convert data to lower case
value = value.toLowerCase();
$.each(terms, function(termIndex, termValue){
// If data contains the term
if (value.indexOf(termValue) !== -1) {
isFound = true;
}
return !isFound;
});
}
return !isFound;
});
return isFound;
}
);
// Perform search
table.draw();
// Disable custom search
$.fn.dataTable.ext.search.pop();
有关代码和演示,请参阅this jsFiddle。