Trim searchtext in jquery datatables using default searchbox

Trim searchtext in jquery datatables using default searchbox

vijayakumarncsevijayakumarncse Posts: 1Questions: 0Answers: 0

I would like the user to do free-text search across all the columns of the data table with server-side property marked as false.Before searching the table how can I trim the search text given by the user?.
I do not want the trim functionality on key-based events as the user would be searching for text with space in between as well.

Ex:
1) 'Micro ' -> 'Micro'
2) 'Micro Soft ' -> 'Micro Soft'

Replies

  • kthorngrenkthorngren Posts: 20,308Questions: 26Answers: 4,769

    You can start with this code and adjust as needed:

          $('.dataTables_filter input')
           .off()
           .on('keyup', function() {
              $('#myTable').DataTable().search(this.value.trim(), true, false).draw();
           });    
    

    It will create a new event handler for the Datatables default global search input. In this case I have regex search on (true) and smart search disabled (false). You will likely want smart search turned off for your requirement. Please see the search() docs for more info regarding regex and smart search.

    Kevin

This discussion has been closed.