Is there any option for "STARTS WITH" condition in client side search ?

Is there any option for "STARTS WITH" condition in client side search ?

EkanthanEkanthan Posts: 6Questions: 1Answers: 0

I need the search results to be "STARTS WITH" condition in client side search. but the results are not as starts with... Please help me out.

Answers

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

    More details, please. Are you talking about DataTables' basic filter, or something else?

  • EkanthanEkanthan Posts: 6Questions: 1Answers: 0

    Yes, About Datatables basic search option

  • allanallan Posts: 62,316Questions: 1Answers: 10,226 Site admin

    Hi,

    Currently DataTables' built in search will match anywhere. I'm thinking about changing it to be a starts with for the next major version, but at the moment you either need to use regex or make a change to the source code.

    Allan

  • EkanthanEkanthan Posts: 6Questions: 1Answers: 0

    Thanks Alan,

    Can you suggest me how to use regex or to change the source code to make this happen.

    Ekanthan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    @Ekanthan, check out the search.regex setting.

    Only caveat is they will have to know regex, and know its enabled. If you want it to search the table for any values that start with something, it will need to start with the carrot symbol ( ^ ) , meaning if you want to match Something, Someone and Somewhere, you would have to search for ^some, the ^ in the beginning will tell regex to match for "Starts with some

    Some other basic regex patterns

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited October 2015

    Actually, in testing this out for you here, I just learned that the DataTables Regex is a little different than I thought.

    I tried to search for ^System(.*), and the console logged errors: /^(?=.*?^System(.*).*$/

    I tried to use that type of regex string at regex101.com, and it alerted the error saying (?= Unbalanced group, (Even though I selected to use the JS regex engine).

    Im assuming the (.*) is whats messing with it...

    Either way, since DT prepends characters to the beginning of the search statement, that means the carrot is out... Im sure @allan has another way

    But here is a good regex example, using the DT API, right on the datatables examples page

  • allanallan Posts: 62,316Questions: 1Answers: 10,226 Site admin

    Use \b at the start of your regular expression to match it match at the start of a word (i.e. a "boundary").

    It is this function in the DataTables source that would need to be modified for this to support it internally. I haven't looked into exactly what change would be needed though. - Possibly adding \b in the concatenation.

    Allan

  • EkanthanEkanthan Posts: 6Questions: 1Answers: 0

    Thanks Allan & jLinux.

    I will try it and update here later

  • EkanthanEkanthan Posts: 6Questions: 1Answers: 0

    Hi

    This is the function in the source code and tried with using \b at the starting of regex. but its still working as the same as before. Can you please modify perfectly with that \b condition.

    function _fnFilterCreateSearch(t,e,n,i)
    {
    var a,s;return n?(a=e?t.split(" "):_fnEscapeRegex(t).split(" "),s="^(?=.*?"+a.join(")(?=.*?")+").*$",new RegExp(s,i?"i":"")):(t=e?t:_fnEscapeRegex(t),new RegExp(t,i?"i":""))
    }

    Thanks
    Ekanthan

  • allanallan Posts: 62,316Questions: 1Answers: 10,226 Site admin

    I'll try to make some time to look into it in the next few weeks.

    Allan

  • EkanthanEkanthan Posts: 6Questions: 1Answers: 0

    Thanks for the support.

    Ekanthan

This discussion has been closed.