Search specific columns
Search specific columns

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
I am using an external text box to search the datatable using:
$('#tbSearch').keyup(function() {
myTable.search($(this).val()).draw();
});
But this searches entire table and I was wondering if I can restrict it to search two columns.
I am trying to search by site name or zip code, so just need to look at these two columns.
Answers
There is
columns().search()
but it is basically an AND search between the two columns, meaning both columns must match the search term. Instead usesearch.fixed()
. See this example.Kevin
Thank you Kevin, but this does not seem to do what I need, right?
I want to search if what user is typing in either site name OR site zip code columns.
It will work for what you want. Take a closer look at the example. The second parameter
data
contains the data object for the row. The example is comparing the inputs to theAge
column by usingvar age = parseFloat(data[3])
.For your case you will compare the input value to the two columns of interest using the
data
parameter.Here is a simple example that looks for
Ashton
in column 0 or 1:https://live.datatables.net/micuwali/1/edit
Kevin
Thank you. Is this part of data table 1.10.22?
I get "myTableName.search.fixed is not a function" error.
Do you I need a specific library?
I have fixedHeader js and css in my project
Searching online suggested adding fixeHeader: true to datatable initialization; did that and now table is not populated at all!
I thought I was there until I wasn't.
It's a new feature in Datatables 2. It's not part of fixedheader. The V1 equivalent is a search plugin. See this V1 range search example. The search plugin docs have been removed due to phasing out that feature with
search.fixed()
. See the search docs for more details.Kevin
This is what I have based on your suggestion.
tbSearchSite is the textbox to enter search text.
You don't want to initiate the search plugin or
search.fixed()
in thekeyup
event. Look at the examples. They are initialized only once. The event handler then callsdraw()
which will execute the plugin orsearch.fixed()
function. The value of the input is obtained within the function.search.fixed()
won't work with Datatables 1.x as it wasn't introduced until V2.data[2]
anddata[8]
work only if you are using array based data. If you havecolumns.data
defined then use the object key instead of the array index.Kevin