Custom search on multiple columns returns no results
Custom search on multiple columns returns no results
Link to test case: https://live.datatables.net/yenifaqe/1/edit
Debugger code (debug.datatables.net):
Error messages shown: none
Description of problem:
I need to have 2 search fields, the first one searches only on the first column, the second one searches on all columns except specified ones. Some extra code is dedicated to handle multiple tables on the same page, each table having its own id.
What happens is that the first filter works correctly, while the second one always removes all the values in the table, whatever the first filter has removed.
This does not seem to be related to the column definition, as the jQuery selector appears to be correctly excluding the unwanted columns.
Maybe I did not fully understand the way that "search(searchTerm)" works on columns()?
Anyway, I'd be glad to get some help to fix my code so that a user could get lines in the table that both comply with the 2 filters.
Thanks in advance and best regards!
This question has an accepted answers - jump to answer
Answers
Applies the search term to each column individually - not to the columns as a group! Each column would need to match the search term to have it return a match.
If you want to apply a custom search to specific columns, you need to use
search.fixed()
with a custom function. That custom function would need to check for a match on the data for the relevant columns in the row.If your second search were to work over all columns, then you could simply use
search()
(the global search), but if it is only a subset, you need to use the method above.Allan
Allan, thanks for your reply.
Actually I would like to apply the "smart" usual search function to a given subset of columns. Is there any way to reuse it from the
search.fixed()
function?Said differently, it is as if I would apply
search()
(the global search) but only on a given subset of columns, like if I was updating the current table configuration options incolumnDefs
with something like{ searchable: false, targets: ".search-ignore .nameCol"}
, on the fly while the users is typing its search terms. I couldn't find a way to update these config values in the API, is it even possible? I could only find some basic options in thesearch()
function, but none about the searchable columns.No sorry. The global search can be turned off per column by using the
columns.searchable
option, but that will disable built in search (including column search) completely.That said, in this case, possibly you could take that approach and then use a different global fixed search on the individual columns (since it gets access to the full data object for the row). That would probably the way to go.
Where it would fall apart is if you wanted to do the more complex grouped search with two or more groups.
Allan
Thanks Allan for your dedication!
OK, I found a workaround that is probably not very efficient, but in my case it does the job as the table length and width are quite small
I created a hidden column where I copy the content of all the cells against which the search term will be tested. The second search field will search only on this special column.
The only drawbask as for now is that the width of the hidden column is still taken into account by Datatable, leaving an empty space on the right of my table. I'm sure that with
table.column(index).visible(false);
I will be able to fix this.