Filtering jQuery Datatable using Regular Expression

Filtering jQuery Datatable using Regular Expression

SKRSKR Posts: 13Questions: 0Answers: 0
edited October 2012 in DataTables 1.8
Hi Allan,

I was checking how the global filter of a jQuery Datatable works. I entered a filter criteria as 'il oh'. I was then going through the jQuery library file and found that _fnFilterCreateSearch creates a regular expression of the form ^(?=.*?il)(?=.*?oh).*$
This seems to do an AND search over the table data i.e. all the rows containing both 'il' and 'oh' were rendered. I wanted to know how we can modify this regex pattern in order to do an OR search i.e. all the rows containing either 'il' or 'oh' should be rendered.

Replies

  • allanallan Posts: 54,702Questions: 1Answers: 8,556 Site admin
    edited October 2012
    Hi,

    That the point where you would change the regex in order to do an OR filter. There isn't (currently) a method of providing a plug-in which will override the built-in filtering - its only possible to augment it. So if you did want to have only an OR behaviour you would need to modify that expression. I'm afraid I don't know off the top of my head how exactly it should be modified - I suspect it would actually a lot less complicated without the look behinds.

    If you do make the modification, let us know how you get on. As I suggested above, I'm planning on providing a way in which plug-ins could completely replace the built in filtering, so this might be an interesting option.

    Allan
  • SKRSKR Posts: 13Questions: 0Answers: 0
    Hi Allan,

    In order to an OR search I modified this regular expression as follows: ^(?=.*?(il|oh)).*?
    This does OR search over table data i.e. all the rows containing either ‘il’ or ‘oh’ or both are rendered. But before using this expression I had to use jQuery grep() funtion to get rid of blank elements from the search array. On the UI I have given the user the capability to select his choice for filtering the table(AND/OR) and accordingly I have made changes to jquery.datatables.js(version 1.8.1) and my end script.

    Regards,
    Swati
This discussion has been closed.