Filtering Question When Using Search

Filtering Question When Using Search

shawnmichaelshawnmichael Posts: 3Questions: 0Answers: 0
edited March 2009 in General
Hello,

Apologies in advance if I'm missing the obvious but is it possible to search for multiple items in one column?

For example, on the page http://www.datatables.net/

I would like to be able to enter both Firefox and Camino from the browser column and have those items appear in the search results. Is there a way to do this?

Thank you in advance and this is very very cool by the way :)

Replies

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    Hi,

    Yes indeed this can be done. What you can do to achieve this is make use of a regular expression in your search term - for example:

    oTable.fnFilter( "(Firefox)|(Camino)", 1, true );

    The last parameter passed to fnFilter() tells it to treat the search string as a regular expression. This way you can build up some seriously complex filtering :-)

    Hope this helps,
    Allan
  • shawnmichaelshawnmichael Posts: 3Questions: 0Answers: 0
    Thank you so much for the response. Unfortunately, this is way over my little head :(

    Our programmer is out and I'm just the person who makes the stuff look pretty.

    I get the gist of what you're saying, I'm just not sure where (guessing it's the jquery.dataTables.js file) or exactly how to make the change. I'll poke around a bit more and see what I can find.
  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    Okay, so taking it basic zero configuration example (like on this site's home page) you could do something like this:

    [code]
    $(document).ready(function() {
    var oTable = $('#example').dataTable();
    oTable.fnFilter( "(Firefox)|(Camino)", 1, false );
    } );
    [/code]

    That will initialise the DataTable, and then immediately apply the filter you are looking for (note the false as the third argument, I got it the wrong way around in my original post...).

    Of course you will likely want to hook this filtering in user input elements, but this should show the basic idea of how "OR" filtering can be done on a single column.

    Regards,
    Allan
  • shawnmichaelshawnmichael Posts: 3Questions: 0Answers: 0
    You're right, I would do want to make this based on user input (the Firefox/Camino thing was just an example).

    In your code example above, what would I put in place of Firefox and Camino... just empty parens???
  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    Hi shawnmichael,

    You could put any Javascript variable in place of the text I have above. For example to use the input from two input boxes:

    oTable.fnFilter( "("+document.getElementById('input1')+")|("+document.getElementById('input2')+")", 1, false );

    Of course the expression should probably be dynamic for if you have anything in either of those inputs. But that's generally how it works.

    Regards,
    Allan
This discussion has been closed.