add selectable radio button to search

add selectable radio button to search

valdezrvaldezr Posts: 49Questions: 0Answers: 0
edited May 2009 in General
How could I modify the search so that exists two radio buttons that specify search on specific fields instead of all the fields. The form would see like this.

Search [ text ] (o) Field1 ( ) Field2

In this case the content of the search field (text) will be searched only in the Field1.

I hope somebody can help me. Thanks in advance.

Replies

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    Hi valdezr,

    What you can do is make use of the fnFilter() API function ( http://datatables.net/api#fnFilter ). What to do is add an event handler to the radio buttons such that when they are clicked fnFilter() is called with the search parameter that you want, and on the column you want. A keyup event handlers on the search input can also be used to provide the same on-the-fly searching that DataTables provides by default. If you want to replace the standard search box, you can just drop it out of sDom ( http://datatables.net/usage#sDom ) and then put your html into the page as you normally would :-)

    Hope this helps!
    Allan
  • valdezrvaldezr Posts: 49Questions: 0Answers: 0
    I don't understand very well.
    Could you provide me a more detailed example of how to create the handle and invoke the fnFilter()?

    Thanks a lot.
  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    Hi valdezr,

    I don't particularly want to just give you to code for this (it's a learning experience for everyone ;-) ), but this is the basic idea for what is needed:

    [code]
    $('#input_switch').keyup( function () {
    if ( document.getElementById('radio_1').checked )
    {
    oTable.fnFilter( this.value, 0 );
    oTable.fnFilter( "", 1 ); /* clear search on col 1 */
    }
    else
    {
    oTable.fnFilter( "", 0 ); /* clear search on col 0 */
    oTable.fnFilter( this.value, 1 );
    }
    } );
    [/code]

    Does that help you get going?

    Allan
  • valdezrvaldezr Posts: 49Questions: 0Answers: 0
    Thanks for your responses.

    I am using now quite well the fnFilter function but now How can I get the text used as parameter in the fnFilter function taken from the filter text box. I will put a new discussion on this topic.

    Thanks again
This discussion has been closed.