datatables.mark.js
datatables.mark.js copied to clipboard
Support RegExp search
As users can search a table with a custom RegExp using e.g. the regex boolean, datatables.mark.js should also handle it as RegExp. In order to implement this, it's necessary to determine if a search was done using the regex option. As there is currently no method to detect this (see this comment), we'll need to wait for a release that implements such method.
This will resolve https://github.com/DataTables/Plugins/issues/136
The same problem is when you use the exact match selector "part1 part2" (space between). Nothing highlighted despite the results.
@aleyazda please open a new issue and provide a fiddle because there are several options to affect that behavior
Hi @DataTables, is there anything new on a method to detect if a search was done using the regex option?
No sorry. Its in the private settings object, but isn't currently available via the public API.
Is there anything I can do to speed this up, e.g. providing a PR?
I really need to sit down sometime and design what I want the new API for the search to look like. This probably won't come until the next major release of DataTables (be it 1.12 or 2.0 - haven't decided yet!).
In the short term what you could do is create a plug-in API method that would read the information you need from the private settings object. The oPreviousSearch
parameter in the settings object contains the information you need (specifically in its bRegex
property. Your plug-in function could just return that.
@DataTables Thanks for your reply. Unfortunately I don't think that this might solve the problem. If I'm right oPreviousSearch
only indicates the settings used for the last search. The plugin on the other hand is able to handle global or column specific searches. This means that if a user searches in two columns I would only be able to retrieve the information for one column. Am I right?
oPreviousSearch
contains information about the global search that is currently applied to the table. Information about the search applied to each column is in the aoPreSearchCols
property - which is basically just an array of object matching the structure of the oPreviousSearch
one.
Allan
@julmot Related, I am trying to support both global search and column search in the same grid, and your highlighter thwacks the global search highlighting. I will whip up a demo later today, but just thought to see if you have a proof-of-concept of using both searches simultaneously.
@jzabroski Thanks for reporting. Since this isn't related to this issue, please create a new one.
Any news on supporting regex option with datatables.mark.js
searching: true, search: { regex: true },
Here is an exemple: https://jsfiddle.net/PBrockmann/zh8006zr/ If you search for 'rancis' you get all San Francisco and highlights If you search for 'ran.is' you get all San Francisco but no highlights
@DataTables
No sorry. Its in the private settings object, but isn't currently available via the public API.
Did this change in the last two years?
No sorry. I guess a workaround would be to use an API plug-in which can read the private property. Do you want the global property or per column?
@DataTables Both, because both is possible and highlighting should behave the same.
This should do it:
$.fn.dataTable.Api.register("search.isRegex()", function() {
var ctx = this.context;
return ctx.length !== 0 ? ctx[0].oPreviousSearch.bRegex : undefined;
});
$.fn.dataTable.Api.registerPlural(
"columns().search.isRegex()",
"column().search.isRegex()",
function() {
return this.iterator("column", function(settings, column) {
var preSearch = settings.aoPreSearchCols;
return preSearch[column].bRegex;
});
}
);
Very simple live example: http://live.datatables.net/mazorupe/1/edit .
@DataTables Thanks for this example 👍 Finally, can you please point out a reference to the .register()
method as I couldn't find it (https://datatables.net/reference/type/DataTables.Api). I'd like to slightly modify your example to use ES6 (especially arrow functions instead of function
, therefore I need to know the available parameters).
Its documented here.