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

大憨熊 提交于 2019-12-11 03:38:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!