Search start with letter

Search start with letter

geethunimeshgeethunimesh Posts: 25Questions: 9Answers: 0

Is it possible to filter rows with start with letter?
I tried it with regex
table.search('^a', true, false).draw(); //select all rows start with a
But it only works for first column
Please help me to find a solution

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin
    Answer ✓

    Try: table.search('\ba', true, false).draw(); - i.e. a boundary search.

    That is actually something I plan to have in the next DataTables core.

    Allan

  • geethunimeshgeethunimesh Posts: 25Questions: 9Answers: 0

    That doesn't work for me allan :(
    jsfiddle.net/ebRXw/3510/
    if I use '^' it will return rows for first column search only

  • geethunimeshgeethunimesh Posts: 25Questions: 9Answers: 0

    I think it is working now
    jsfiddle.net/ebRXw/3511/
    var regExSearch = '\b' + this.value;

  • geethunimeshgeethunimesh Posts: 25Questions: 9Answers: 0


    Sorting updates search box with /b
    "\b33"

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    var regExSearch = '\b' + this.value;

    Yes, that should do it. The \b is a boundary, which is the correct action to use rather than ^ to indicate the start, since the way the search works in DataTables is to combine all the searchable columns into a single string.

    Allan

  • geethunimeshgeethunimesh Posts: 25Questions: 9Answers: 0

    So Can we eliminate this "\b" on sorting?
    Actually, I don't wish to appear this while sorting. Any suggestion?

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    I thought we were talking about search, not sort. The \b is a regex special character. It shouldn't effect the sort at all.

    Allan

  • geethunimeshgeethunimesh Posts: 25Questions: 9Answers: 0

    @allan Now I'm manually removing "\b" while sorting. Any other better option to solve this?

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    I don't really understand. What has the \b got to do with sorting? Can you link to a test case showing the issue please? The \b is in the filter input only - it shouldn't be in the actual data.

    Allan

  • geethunimeshgeethunimesh Posts: 25Questions: 9Answers: 0

    @allan
    Step to reproduce:
    1. type a text in search field
    2. click on any column to sort
    3. check search field ['\b' will be prepended with search text]
    One more issue is there
    1. type "*d" in search field
    2. get console error and no search will take place

    Any suggestion?

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    You've got an internal loop there. Calling search() from the built in search box will cause it to update itself, which results in the search happening, which... loop.

    If you want to use the built in input, you'd need to unbind all of its existing listeners:

    $('.dataTables_filter input').off();
    
    1. get console error and no search will take place

    The console error is correct - *\b is not a valid regex.

    Allan

This discussion has been closed.