How to search a word with whole column data matching the word ?

How to search a word with whole column data matching the word ?

sahildcodersahildcoder Posts: 6Questions: 2Answers: 0

For eg. I have data in column as John Mary. and I dont the result to contain this record when either 'John' or 'Mary' is searched.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,206Questions: 1Answers: 2,592
    Answer ✓

    Hi sahildcoder,

    There's a few ways to do this.

    If you want this search to be from the DataTables input box, then

    1) if you only want to search on a specific column, you'll need something like this (see example):

    $(document).ready( function () {
      var table = $('#example').DataTable();
    
       $('#example_filter input').on( 'keyup', function() {
         var mySearch = '^' + $(this).val() + '$';
         console.log(mySearch);
          
         table.column(0).search(mySearch, true, false).draw();                                  
      });    
    });
    

    2) if you want to search across all the columns, then you'll need a search plugin (see example):

    $(document).ready( function () {
      var table = $('#example').DataTable();
    
        
      $.fn.dataTable.ext.search.push(
        function( settings, searchData ) {      
          if ( $('#example_filter input').val().length == 0 ) {
            return true;
          }
          
          for (i = 0; i < searchData.length; i++ ) {
            if ( searchData[i] == $('#example_filter input').val() ) {
              return true;
            }
          }
          
          return false;
        }
      ); 
    });
    

    However, if you want to search in an individual input connected to a column, look at this example.

    Hope that helps,

    Cheers,

    Colin

This discussion has been closed.