Filter table with exact match
Filter table with exact match
Alex2019
Posts: 62Questions: 9Answers: 0
in DataTables
Hello everybody
i have this code that should filter a table
if I click on the Alex checkbox nothing happens
but if I click on Alex 2 the filter will be executed
the problem is that the code is not doing an exact search
where is the error in the code?
<script>
// start filter checkbox 2 di 2
$('input:checkbox').on('change', function () {
//build a regex filter string with an or(|) condition
//build a filter string with an or(|) condition
var credit = $('input:checkbox[name="cred"]:checked').map(function() {
return this.value;
}).get().join('|');
//now filter in column 2, with no regex, no smart filtering, not case sensitive
table.column(4).search(credit, true, false, false).draw(false);
//
</script>
This question has accepted answers - jump to:
Answers
You have regex searching enabled:
Your search string looks to be just
Alex
orAlex 2
. With regex that will match both names. If you wantAlex
to match onlyAlex
then you need to do something like this^Alex$
. You can try different regex patterns with this example.Kevin
Thanks for the reply kthorngren
can you help me with the code ?, it's too complicated there
I'm not that experienced (I'm not a programmer)
Try this:
Kevin
does not work
alternative solution?
does not work
We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
I didn't look that close at your actual data - I thought it was "Alex" and "Alex 2". But its actually "Test movie per Alex" and "Test movie per Alex 2". If the exact match you want is always at the end of the string then you can remove the
"^" +
on line 2. If the exact match can be in the middle of the string then it will be very difficult, I think, to distinguish between "Alex" and "Alex 2" with an exact match. The solution I provided is an exact match for the full string.Kevin
it should work like this
example
Alex, Alex 2, Alex 3
Alex filter only Alex and exclude the others
Alex 2 filter only Alex 2 and exclude the others
Alex 3 filter only Alex 3 and exclude the others
The example I gave will do that if your cell data contains only the text you are trying to filter. For example:
http://live.datatables.net/xoduxaye/2/edit
But your data, in the first post, includes other text than the exact match string you are trying to filter. Your exact match will need to be "Test movie by Alex" to match only that row. Otherwise the regex search will match both rows with "Alex" and "Alex 2". There is not a simple solution to have "Test movie by Alex" and "Test movie by Alex 2" and have an exact match of just "Alex" show only "Test movie by Alex". Likely a Search plugin will need to be written to support this type of matching.
Kevin
Hi kthorngren,
strange, in your code (which is I replicated) everything works
but in mine the data is taken from a mysql database and processed in the php code and then passed to the checkbox
column 0 'Movie Title' is not taken into account
and column 4 "Credited 'which must be filtered, and also specified in the code
table.column (4) .search (credit, true, false, false) .draw (false);
but it doesn't happen
Can you link to your page please so we can debug it?
Colin
Hi colin
sorry i'm not on the internet, everything works in localhost
but I can post the piece of code in php that queries the database and executes everything
It looks like your data has extra text, ie,
Alex (52)
andAlex 2 (12)
. If this is consistent then the regex expression can be updated to include that part of the string. Something like this should workreturn "^" + this.value + "\\W\\(\\d+\\)";
. Note the double backslashes. The backslashes need to be escaped so they are part of the string. See this updated example:http://live.datatables.net/xoduxaye/4/edit
A good place to workout regex expressions is https://regex101.com/ . I tested the regex expression here:
https://regex101.com/r/tst0sf/1
Use the above to update and test the regex expression if the above doesn't work with your data. Make sure to use exact examples of your data.
Kevin
Hi kthorngren
i found the error, and in the php code, exactly here
in the same column I entered the credited and birthday film_year, the latter calculates the release of the film and the birthday of the actor and returns how old he was when he starred in that film
the regex works
now I have to think how to fix it, I would not want to create another column
i have tested this and it doesn't work
return "^" + this.value + "\\ W \\ (\\ d + \\)";
the reason is why after credited there is **<br> **
so select everything
it should be so
You can have a hidden column and search the hidden column. Use
columns.visible
to hide the column.Looks like you have spaces following the backslashes. There should not be spaces. Not sure what you mean by the
<br>
comment.If you want further help for this please build a test case that replicates your exact data so we can provide a more specific answer. Otherwise use https://regex101.com/ to workout the appropriate regex.
Kevin
<td><?php echo $row->credited;?>
<br>
<?php
$var1 = $row->birthday;
Maybe change
\W
to\W+
or maybe better\s+
for one or more matches. See the regex help for details of what these mean.Kevin
look
after the name is present <br>
as you can see it doesn't work
http://live.datatables.net/xoduxaye/6/edit
Like I said you have extra spaces after the backslashes (and other places) in your regex expression. So regex is looking for literal spaces. Also, since you have more than one whitespace character between the name and the
(52)
you need to add a+
. Either replace the\W
with\W+
or use\s+
. See the updated example:http://live.datatables.net/xoduxaye/7/edit
Kevin
it works
thank you kthorngren
you were very kind to help me
kthorngren hi
can you kindly explain this regex to me?
The regex101 site will explain it better than I can
Take a look at the regex101 test case:
https://regex101.com/r/tst0sf/1
The top right provides the details of the regex. The bottom right has a quick reference so you can see other tokens. There are lots of regex tutorials online that you can use to learn regex.
Kevin
Thanky