column.search() with an Html tag
column.search() with an Html tag
![ThomasHer](https://secure.gravatar.com/avatar/52fd0ac1de3569bf6e303d547d9abe49/?default=https%3A%2F%2Fvanillicon.com%2F52fd0ac1de3569bf6e303d547d9abe49_200.png&rating=g&size=120)
Hi !
I have a problem with the search function. My table has 2 Html tags inside a <td> tag and would like to search inside these tags to display the row I want. Here is the HTML for 1 cell
<td><checkOrCross><i class="fas fa-times w3-text-red"></i></checkOrCross></td>
The tag <checkOrCross> is an helper generated in the server-side. I would like to have a filter with a <select> so I did that
<select>
<option value="" selected></option>
<option value="True">Oui</option>
<option value="False">Non</option>
</select>
$('select').on('change', function () {
var val = $(this).val();
var classExpected;
if (val == "False")
classExpected = "fa-times";
else if (val == "True")
classExpected = "fa-check";
else
classExpected = "";
if (table.column(-1).type == "html")
table
.columns(-1)
.search(classExpected)
.draw();
});
As you can see, I want to search the class name of my <i> which only display a cross or a check.
But, because it's inside an HTML tag, the search() method can't find all the attributes. I tried this
table
.columns(-1)
.search("<checkOrCross")
.draw();
But it failed. I tried with regular expressions but it was not a success neither.
so my question is : How can I use the search() function to search inside a Html tag / class name of a tag ?
at least, is it possible ? Do I have to escape some characters ?
thank you for your help.
Answers
The way to do this is with an orthogonal data renderer. When DataTables asked for "search" data, pull the class name out of the HTML string (a regex should do it, or perhaps a simple
.indexOf
if the string is known to be unique) and return what you want DataTables to search on.Regards,
Allan
Ok but i'm beginner with this plug-in (and also with javascript) so how am I suppose to get the HTML string of each row ?
I don't know how to access to the data when I got the column
Hi @ThomasHer ,
This example here may help - it's showing how the column's data can be tweaked before being displayed.
Cheers,
Colin
Hi @colin
Yeah thank you, it helped me !
I did that :
And it works ! But the problem is that it replace my icon, in my array, by the string returned in commentary. So I tried to use "filter" instead of "render" but it doesn't work.
I have searched how to do with the link of @allan (thanks by the way) but i'm steal at the same point.
I am trying to access to the columnDefs to if I can see the data but it's also a failure. If it doesn't return an error, it's undefined. I tried :
Is the field filter not supposed to be used automatically called when I use search() ?
Hi @ThomasHer ,
Yep, it's definitely the
columns.render
you want. The function can be multi-purpose and return different data for a cell depending on whether it's for a filter, search or just display. This example here for example returns different values for display (it makes a URL) than it does for everything else. This may be the way to go for you,Cheers,
Colin
Hi @colin ,
Yeah I tried a lot of things with render, but it doesn't work. the problem is that the search function can't search occurences inside html tags. It can't search the html tag's name itself !
With render, I try to return
But it cannot even find "i". If i return
It will only search in the "random" string
The problem is that my <td> has nothing except the <i> tag and I cant put anything inside it because I only want the icon in my <td>.
So i'm lost. I don't see how can I use your tools to filter my column.
Hi @ThomasHer ,
Probably the best bet here is if you create a live example/fiddle with what you've got (the data and the code), and saying what you want, then we'd have something to work with,
Cheers,
Colin
Bit late to the party, but I stumbled on this thread regarding the exact same issue (using font-awesome icons instead of text values), so I thought I'd post my solution for anyone who drops by.
Caveat: This uses Dropdown filters (<select>) to apply filtering, but the solution should work wherever you call the search() function.
The short version: Use a hidden <span> element that contains a string to search by using RegEx/Smart Search. You'll need to apply additional css if you need to hide it on printing etc.
The long version:
This builds on another post (which I can't be bothered to look for) regarding filtering columns with drop-downs. My apologies for not giving the original author credit! It's been a while since I wrote the original callback.
This could probably be written better, anyone who wants to is welcome to update the code if needed.
In your datatables options do something similar to the following:
This will build dropdown selects for any columns that you specify in the array for the .columns([array]).every() method, and will support icon-only columns in the following format:
<i class="fa fa-fw fa-check"></i><span class="hidden">_YOUR HIDDEN TEXT TO SEARCH AGAINST HERE_</span>
Replace fa-check (or fa-times), with whatever icon you are using wherever you see it in the code above.
Obviously this isn't supported behaviour for DataTables, however on exporting to excel only the text value is displayed as the html is stripped out, hence why I use "True" and "False" as my hidden text string.
Hope this helps anyone else who swings by!