jQuery DataTables 'OR' Search/ Filter

后端 未结 4 959
感情败类
感情败类 2020-12-09 05:25

I am using jQuery DataTables (http://www.datatables.net/) to display some tabular data. The search/ filter is a powerful feature. Although if multiple keywords are searched

4条回答
  •  再見小時候
    2020-12-09 05:56

    AND-filter (include rows where all search words is present). This custom filter overrides the builtin filtering process. Each column in each row is compared with each search word.

    $.fn.dataTableExt.afnFiltering.push(
      function(oSettings, aData, iDataIndex) {
          var keywords = $(".dataTables_filter input").val().split(' ');  
          var matches = 0;
          for (var k=0; k-1) {
                      matches++;
                      break;
                  }
              }
          }
          return matches == keywords.length;
       }
    );
    

    forked fiddle -> http://jsfiddle.net/9d097s4a/


    OR-filter (include rows where at least one of the search words is present). This is another approach, mostly an attempt to streamline this answer above. Instead of playing with oSearch and hardcoded search terms, the default filtering event is replaced with an event that tokenizes the search phrase and then performs an advanced fnFilter(). As optional experiment, the search is now only performed when the user hits enter - it feels somehow more natural.

    var input = $(".dataTables_filter input");
    input.unbind('keyup search input').bind('keypress', function (e) {
        if (e.which == 13) {
           var keywords = input.val().split(' '), filter ='';
           for (var i=0; i

    see demo -> http://jsfiddle.net/2p8sa9ww/

提交回复
热议问题