Negative Exclude Except search example

Negative Exclude Except search example

ACAC Posts: 2Questions: 0Answers: 0
edited March 2011 in General
A custom filter that uses the built-in filter input and provides negative search feature.

If the first character of the search is an exclamation point ( ! ), items that contain the rest of the string will be excluded.

A search for "Safari" shows all records that match.
A search for "!Safari" shows all records that do not match.

This example requires the jQuery typeWatch plugin to delay filtering for one second.
[code]
table = $('#example').dataTable();

var options = {
callback: myfilter,
wait:1000,
highlight:true,
captureLength:2
};

function myfilter() {
var srchval = $('input').val();
if(srchval.substr(0,1)=='!'){
table.fnFilter( '^(?:(?' + srchval + ').)*$', null, true, true,false);
} else {
table.fnFilter( srchval, null, false, true,false);
}
};

var input = $('#example_filter > input');
input.unbind('keyup');
input.typeWatch(options);
[/code]

Replies

  • ACAC Posts: 2Questions: 0Answers: 0
    edited May 2011
    To allow the list to be revert back to unfiltered, modify the typewatch js file.

    jquery.typewatch.js 2.0 version:

    Find the line that contains:
    if ((elTxt.length > options.captureLength

    modify it to:
    if ((elTxt.length >= options.captureLength

    In the options code above, set captureLength: 0

    Now
    A search for "Safari" shows all records that match.
    A search for "!Safari" shows all records that do not match.
    Clearing the search box will revert to all records shown.
This discussion has been closed.