search box

search box

bilbodigibilbodigi Posts: 18Questions: 0Answers: 0
edited May 2009 in General
Hi Allan
How can i use regular expressions in the default search box?
I want to search for firefox OR Mozilla OR win, and show all results that match.

Replies

  • allanallan Posts: 63,113Questions: 1Answers: 10,397 Site admin
    Hi bilbodigi,

    What you can do is set the internal variable that DataTables uses to see if it should escape regex characters or not:

    oTable = $(...).dataTable(...);
    oTable.fnSettings().oPreviousSearch.bEscapeRegex = false;

    Then you should be able to enter regular expression characters into the standard search box.

    Hope this helps,
    Allan
  • abraamabraam Posts: 17Questions: 0Answers: 0
    edited August 2009
    Allan
    i tried this and i have this problem
    when i enter ??????| (AGGL|) nothing happens
    in regular regex filtering the same thing returns all data
    when i enter ??????|??? (AGGL|MAT) it only searches for ?????? (AGGL), but if i write ??????|???? (AGGL|MATH) and press a backspace to erase last character then it returns the right results, that is everything with ?????? or ??? (AGGL or MAT). It seems that it is working properly after backspace and not with each character.
    Is there something else i should change? ( http://www.thelwnamathw.gr/vaseis/ )

    i tried also to use in my page your example with the extra regex filters but the 4 input spaces never appeared.
    Any clues?

    --edit--
    i tested that with greek characters, but i cant write them here. i added in parenthesis something similar with latin characters
  • allanallan Posts: 63,113Questions: 1Answers: 10,397 Site admin
    Hi abraam,

    (Going via ASCII - not sure why UTF-8 won't sure up here - probably the forum software - I'll look into that...) - AGGL| in regex means "AGGL or " (i.e. nothing). Therefore it's not actually filtering on anything.

    I think I know what is happening with the search... What is happening is that DataTables uses a 'fast filter' whereby a new search is performed on the old search results. For example if I enter "Appl", and then "Apple" there is no point is searching for "Appl" again since we already have that result, so we can just look in there for Apple.

    However, this falls apart when you introduce a regular expression OR. The subset of AGGL won't mean anything to the subset MAT.

    Try this as a possible fix - lines 2974-2976 (1.5b11) - replace the condition with:

    [code]
    if ( oSettings.aiDisplay.length == oSettings.aiDisplayMaster.length ||
    oSettings.oPreviousSearch.sSearch.length > sInput.length || iForce == 1 ||
    sInput.indexOf(oSettings.oPreviousSearch.sSearch) !== 0 ||
    (sInput.indexOf('/') !== 0 && oSettings.oPreviousSearch.sSearch.indexOf('/') === 0)
    )
    [/code]
    Does that work for you?

    Regards,
    Allan
  • abraamabraam Posts: 17Questions: 0Answers: 0
    Hi allan
    i tried that and nothing changed. I have noticed that a backspace (or delete) can make the search work right.
    But i use minified version so the new code i used was this

    [code]oSettings.aiDisplay=oSettings.aiDisplayMaster.slice()}else{if(oSettings.aiDisplay.length==oSettings.aiDisplayMaster.length||oSettings.oPreviousSearch.sSearch.length>sInput.length||iForce==1||sInput.indexOf(oSettings.oPreviousSearch.sSearch)!==0||(sInput.indexOf('/') !== 0 && oSettings.oPreviousSearch.sSearch.indexOf('/') === 0)){oSettings.aiDisplay.splice(0,oSettings.aiDisplay.length);/*PROsthesato||(sInput.indexOf('/') !== 0 && oSettings.oPreviousSearch.sSearch.indexOf('/') === 0)*/
    [/code]
    I hope i changed the right thing!
  • allanallan Posts: 63,113Questions: 1Answers: 10,397 Site admin
    Hi abraam,

    I've just realised that there was a mistake in the code I posted - where I have a '/', it should of course be a '|'. However, looking at it further, it's not actually quite as easy as I at first though :-). The trick here is DataTables 'cunning' filtering - which it breaks the search down by word. As such the OR would only apply on the same word! This might be good enough for you, but it's not a solution that I'm going to incorporate into DataTables proper...

    You fix looks fine for what you want to do - so I would go with that. If you wanted to do it without altering DataTables, what I would suggest instead is to use the plug-in filtering API and have your filter run on that rather than the built-in filter (thinking I should build a demo of that some time as this kind of thing has cropped up a couple of times!).

    Regards,
    Allan
  • abraamabraam Posts: 17Questions: 0Answers: 0
    Allan that demo would be perfect!
    But as usual something ne w comes up. I managed to change the page into server processing (the diference in speed is huge) but on that example

    oTable = $(...).dataTable(...);
    oTable.fnSettings().oPreviousSearch.bEscapeRegex = false;

    doesn't work at all. I mean that no regex is accepted when i use server side processing with that trick.
    my code is:
    [code]$(document).ready(function() {
    oTable = $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "server_processing.php",
    "bAutoWidth": false,
    "bSortClasses": false,
    "aoColumns": [ { "sWidth": "560px"}, { "bSearchable": false, "sWidth": "40px" }, { "bSearchable": false, "sWidth": "40px" } ]

    } );
    oTable.fnSettings().oPreviousSearch.bEscapeRegex = false;

    } );[/code]

    see that working at http://www.thelwnamathw.gr/servtest/serv.html
  • allanallan Posts: 63,113Questions: 1Answers: 10,397 Site admin
    Hi abraam,

    Ah yes, but with server-side processing, all the searching (including regular expressions) are dealt with by the server, and not DataTables :-). DataTables will send the variables 'bEscapeRegex' and 'bEscapeRegex_[column]' to indicate if the server should use regex or not. Note that I have not put regex support into my example server-side script because regex in MySQL is horrible. So it's up to you, the developer, how you deal with regex on the server side :-). Over to you ;-)

    Allan
  • fliesflies Posts: 35Questions: 0Answers: 0
    It maybe harsh on a server but it would be better to just get all rows and regex them internally via php. But if you have 10000+ records as I said a harsh job for sql...
  • abraamabraam Posts: 17Questions: 0Answers: 0
    @Allan
    well Allan, i hope some day i will become a developer. For the time being i feel like Sherlock Holmes searching for clues in google and forums. Thanks for all the fast responses.
    @flies
    My data is 526 records but the server side processing is real fast and i will most certainly keep it that way.

    To the point, all i really need is multiple OR. So i think i should separate the search strings and make a search with them in mysql. I wont need more of the regex stuff. Of i go for new search...
  • allanallan Posts: 63,113Questions: 1Answers: 10,397 Site admin
    Hi abraam,

    With only 526 records, DataTables should be able to handle that fast enough on it's own - but I'd recommend turning off bSortClasses, which can seriously slow things down in large tables. Either that, your you can find some way of making your search do what you want to in SQL. Probably best to ask that one in some SQL forum.

    Regards,
    Allan
  • adromiladromil Posts: 53Questions: 4Answers: 0
    Can I make my own search box like for example I wanted to use a separate or
    Search...

    control?
    Hope you can help.
    -thanks Ü
  • allanallan Posts: 63,113Questions: 1Answers: 10,397 Site admin
    Sure you can :-). Just create the SELECT element as you normally would and then use fnFilter on the 'change' select for the SELECT.

    Allan
This discussion has been closed.