Search api for word NOT in cell

Search api for word NOT in cell

EMORRASEMORRAS Posts: 10Questions: 2Answers: 0

Hi,

In my table column "State" I have 3 types of 'val', "Pending", "Delivered" and a big mix of other states.

I implemented 3 checkboxes to show rows with "Pending", "Delivered" and "Others", if "Pending" is checked it shows all rows with "State" column having "Pending", similar with "Delivered"

I use

function doSearch ( col, val ){
  $('#myTable').DataTable()
  .column( col )
  .search( val )
  .draw();
}

$('input.pending').on('click', function(){
  if ( $(this).prop('checked') ){
    doSearch( $(this).attr('data-column') , "Pending" );
  }
  else{
    doSearch( $(this).attr('data-column') , "" );
  }
});

Now I want to show the "Others" rows that haven't "Pending" nor "Delivered" state. I tried with regular expressions with this

function doNOTSearch ( col, val1, val2 ){
  $('#myTable').DataTable()
  .column( col )
  .search( val1, true, false, false )
  .search( val2, true, false, false )
  .draw();
}

$('input.others').on('click', function(){
  if ( $(this).prop('checked') ){
    doNOTSearch( $(this).attr('data-column') , "^((?!Pending).)*$",  "^((?!Delivered).)*$" );
  }
  else{
    doSearch( $(this).attr('data-column') , "" );
  }
});

But can't make it work. Is there another way to search rows except those that have a given value?

Thanks

This question has an accepted answers - jump to answer

Answers

  • EMORRASEMORRAS Posts: 10Questions: 2Answers: 0

    I tried other combinations of regular expressions "^((?!Pending).(?!Delivered).)*$" .

    Should/Could I use the filter() api with a custom function?

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    You need to combine the two conditions into a single regular expression rather than trying to chain them together with two search() calls. Initially I was puzzled as to why that wasn't throwing an error, but thinking about it, the search() call on line 5 above is actually performing a global search, rather than a column specific search, hence why you aren't getting the results you might expect.

    Try:

    table.column( col ).search( '^((?!Pending|Delivered).)*$', true, false ).draw();
    

    Example: http://live.datatables.net/ruyotora/1/edit

    Allan

  • EMORRASEMORRAS Posts: 10Questions: 2Answers: 0

    Thanks for the answer Allan, works perfect. The search should perform a search on the given column. I'll dig on it and ask again if can't

  • EMORRASEMORRAS Posts: 10Questions: 2Answers: 0

    I understand, the search that performs a global search is the second one, not the first one.

    function doNOTSearch ( col, val1, val2 ){
      $('#myTable').DataTable()
      .column( col )
      .search( val1, true, false, false ) <<<==== THIIS SEARCH IS COLUMN
      .search( val2, true, false, false ) <<<==== THIIS SEARCH IS GLOBAL
      .draw();
    }
    

    Thanks

This discussion has been closed.