问题
Lets say I have two rows in the table all with a single column:
hello mr red
goodbye morpheous
I want to be able to filter for "hello morpheous" and have it return both rows.
By default it does not do this? It will only return the row containing "hello".
Can can I achieve this? Thanks :)
回答1:
You simply create a custom filter like this :
$.fn.dataTableExt.afnFiltering.push(
function(oSettings, aData, iDataIndex) {
var filter = $("#example_filter input").val();
filter = filter.split(' ');
for (var f=0;f<filter.length;f++) {
for (var d=0;d<aData.length;d++) {
if (aData[d].indexOf(f)>-1) {
return true;
}
}
}
}
);
This custom filter breaks up the values entered in the filter box splitted by space, then cycle through all columns in all rows - if any of the columns for a row match any of the entered filter criterias, like "hello" or "morpheous", it is a match.
See demo -> http://jsfiddle.net/ca6Zc/ just grabbed an earlier demo used for something else, does not have the time right now to produce a one column example with a lot of rows. But at least it proofs the method is quite satisfying :) Just try enter different letters separated by space in the filter box, like e f 8
etc.
来源:https://stackoverflow.com/questions/22839663/jquery-datatables-filter-multiple-words-and-return-all-rows-that-match-at-least