Is there any option for "STARTS WITH" condition in client side search ?
Is there any option for "STARTS WITH" condition in client side search ?
Ekanthan
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.
This discussion has been closed.
Answers
More details, please. Are you talking about DataTables' basic filter, or something else?
Yes, About Datatables basic search option
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
Thanks Alan,
Can you suggest me how to use regex or to change the source code to make this happen.
Ekanthan
@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 someSome other basic regex patterns
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
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
Thanks Allan & jLinux.
I will try it and update here later
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
I'll try to make some time to look into it in the next few weeks.
Allan
Thanks for the support.
Ekanthan