Is there an option in here where we can do an OR filter by column?
Is there an option in here where we can do an OR filter by column?
Hello. I'm looking to see if there's an option here in dataTables that we can do an OR filter by column?
So for example, lets say I have a table with 3 rows and 3 columns:
Column1 - Column2 - Column3
1 2 3
a b c
4 5 6
And I have:
Search input for Column 1 = a
Search input for Column 2 = 5
Search input for Column 3 = 3
This would in fact return all 3 columns instead of none.
Is there something like this in dataTables? Thanks.
This question has an accepted answers - jump to answer
Answers
You would use a search plugin for this. See if this thread helps:
https://datatables.net/forums/discussion/comment/153553/#Comment_153553
Kevin
Thanks for the help!
The last example: http://live.datatables.net/mucevape/1/edit is close to what I'm looking for. Only two things I would like to change. 1. Can I change the <select> into a general input search where user types in the field? and 2. Can I change the position of that search field outside of the table anywhere on the page? Thanks.
Yes to both. Its just an input. You can use what you want and place them anywhere you want. Your event handler for the inputs will simply need to use
draw()
and in the plugin you fetch the input values and process them as needed.Kevin
I managed to change the position (although it changed both instead of one) as well as changing from 'select' into 'input' however the data is not being filtered on the table. Do you know what could be wrong? Here is the edit: http://live.datatables.net/mucevape/39/edit
If you want text inputs then you wouldn't use any of the code inside the
initComplete
function. That is for select inputs. You will need to create the text inputs and create a different event handler. Here is an example:http://live.datatables.net/lecixuca/3/edit
Kevin
I was hoping for the data on the table to be updated as I type (like the general search). Also, it looks like it only works if it matches 100%. So I'm taking from this that there's no plug in or easy way of doing this? Thanks for the help though!
I showed one simple example. The input you use and the code you use in the plugin is not restricted by Datatables. You can implement the behavior you want.
Instead of invoking the event on a
change
you can usekeyup
which would behave more like the default search. Within the plugin you can use a regex match instead of simply using===
.Kevin
Thinking about it for a bit I realized that doing an OR search across multiple columns might not be that straight forward. You need to be able to properly handle the case of one inout being blank while the other contains input. Here is the example updated with regex search and keyup event:
http://live.datatables.net/hocoyuke/4/edit
The complexity will increase with more columns.
HTH,
Kevin
Awesome! that's definitely what I was looking for. Thanks!