Trim all the datatable elements spaces
Trim all the datatable elements spaces

Dears,
I saw some values in datatable as a space from the origin (ex “item ”). The problem is that when I want to find this element (with a variable without spaces) in datatable with the following instruction It doesn’t mach:
mytable.columns(2).search("^"+element+"$", true, false);
Is it possible to trim all the datatable elements spaces (“ “) ?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
If you are using a database, that would be better done at the database level, if the spaces aren't supposed to be there.
If not a database, where are the spaces coming from?
Hi Tangerine... thanks for your feedback.
You're right. The spaces coming from database level. I'll fix it.
however, is it possible to delete those spaces from datatable?
Thanks in advance.
You can use
columns.render
and use something like trim() to remove the whitespace. See the example here:https://datatables.net/manual/data/renderers#Custom-helpers
But this might have a negative affect on performance if you are doing this for all your columns.
Another option is to change your search. You are using a regex search (
"^"+element+"$"
) that doesn't allow anything other than what is inelement
. You can change it to"^"+element+"\s*$"
to allow 0 or more trailing white space. Or you can use smart search by changing the boolean values:mytable.columns(2).search( element ", false, true)
See the
search()
docs for more details about smart search mode.Kevin
Thanks for your help kthorngren!..
I changed the argument and it worked perfect!.
Regards.
Diego.