Trim all the datatable elements spaces

Trim all the datatable elements spaces

Diego ApuglieseDiego Apugliese Posts: 8Questions: 3Answers: 0

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

Answers

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394

    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?

  • Diego ApuglieseDiego Apugliese Posts: 8Questions: 3Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,320Questions: 26Answers: 4,773
    edited May 2020 Answer ✓

    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 in element. 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

  • Diego ApuglieseDiego Apugliese Posts: 8Questions: 3Answers: 0

    Thanks for your help kthorngren!..

    I changed the argument and it worked perfect!.

    Regards.

    Diego.

This discussion has been closed.