I'm not sure if 'Edinburgh' should be hard-coded, or if you'd like users to be able to specify a value of their own, so here is the hard-coded solution, with comments for user-provided value option:
$.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
var exclude = 'Edinburgh'; // User provided value: $("#user-exclude-field").val();
if(!exclude) return true; // nothing to exclude, so all rows should be included
var re = new Regexp("\\b" + exclude + "\\b", "i"); // using the \b word boundary meta-character to match only "Edinburgh" and not "NewEdinburgh" (typo on purpose)
return !re.test(data.office); // return TRUE (i.e. include this row in the search results) if the "office" attribute of the current row data does NOT match our excluded word
});
UPDATE
So based on your comment, here's a sample fiddle:
https://jsfiddle.net/bjLzkjnc/
UPDATE 2
So you want to instead be able to filter for all items that contain a word, or filter for all rows that do not contain that word. Update fiddle: jsfiddle.net/1xh3kys5
The magic here is the filter
variable. Before it was just a boolean variable because we wanted to track two states: "contains SC" vs "show me everything". But now you actually have three states: "contains SC", "show me everything" and "show me everything that does not contain SC"... So a boolean is no longer enough.
There are a few ways to approach this, but I went with a filters object, so that you could eventually add more filters easily:
var filters = {
sc: {active: false, re: new RegExp("\\bSC\\b", "i"), include: true}
};
The filters object here has only one attribute, one filter, called "sc". That attribute is itself an object, that has a few attributes that will let us represent all of our three states:
- active is to turn this particular "sc" filter on/off. So when you want to show all rows, you simply set this to false, and deactivate the sc filter.
- include is our boolean to control whether we want to include or exclude rows that contains the word "SC".
- re is simply the regular expression we will use when we want to filter the rows.
When we filter rows now, we simply use the regular expression to determine if a particular row contains "SC".
var includes_sc = filters.sc.re.test(data[2]);
return (filters.sc.include ? includes_sc : !includes_sc);
So, based on the "include" attribute's value, and store it in a variable called includes_sc
. If the current filter wants all rows that include "SC", we return includes_sc
as is. If, on the other hand, we want all rows that do not include "SC", we return the inverse (!includes_sc) of includes_sc
.
Hope this helps!