简体   繁体   中英

jQuery DataTables filter multiple words and return all rows that match at least one of them

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 :)

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 ef 8 etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM