Multiple terms filtering

Multiple terms filtering

mikemakusmikemakus Posts: 9Questions: 0Answers: 0
edited September 2009 in General
Hi,

I'm trying to implement a multiple terms filtering on my datatable. I have three select dropdowns that have an onchange event which triggers fnFilter with the selected value. Each one filters the table according to the selected value, but what I would like is that all three selected terms get filtered.

I guess, simply put, is there a way to filter for example : apple + pears + bananas
Which would show only the table rows that have these three terms.

Thanks for the help

Replies

  • allanallan Posts: 61,761Questions: 1Answers: 10,111 Site admin
    Hi mikemakus,

    All you need to do is concatenate your search terms together, and have them space separated. In your example you would do: fnFilter( "apple pears bananas" ); and that would filter to just row with those three terms.

    Regards,
    Allan
  • mikemakusmikemakus Posts: 9Questions: 0Answers: 0
    Hi allan,

    Thanks for the quick reply.
    How can I transpose that to my onchange functions ?

    Right now, what I have is three onchange events, one for each of my select dropdowns :

    [code]$('#dropdown_1').change( function() { oTable.fnFilter( $(this).val() );} );[/code]

    I guess I need a variable to store the values of the three selected elements, and then a button to throw that into the filter. Any idea as to how I could do that ?

    Thanks
  • allanallan Posts: 61,761Questions: 1Answers: 10,111 Site admin
    Hi,

    Can you not just get the value of each dropdown in the change event? So for example:

    [code]
    function fnThreeWayFilter()
    {
    /* Get the filter values */
    var filter1 = $('#dropdown_1').val();
    var filter2 = $('#dropdown_2').val();
    var filter3 = $('#dropdown_3').val();
    var filter = "";

    /* Concat them together */
    if ( filter1 != "" )
    {
    filter += filter1+" ";
    }

    if ( filter2 != "" )
    {
    filter += filter2+" ";
    }

    if ( filter3 != "" )
    {
    filter += filter3+" ";
    }

    /* Drop the last space */
    if ( filter != "" )
    {
    filter = filter.replace(/ $/, "");
    }

    oTable.fnFilter( filter );
    }

    $('#dropdown_1').change( fnThreeWayFilter );
    $('#dropdown_2').change( fnThreeWayFilter );
    $('#dropdown_3').change( fnThreeWayFilter );
    [/code]

    Of course, there are many other ways to do this - but this is one option if you have full control over the three select elements :-)

    Regards,
    Allan
  • mikemakusmikemakus Posts: 9Questions: 0Answers: 0
    You made my day sir, thanks a lot ! I'll try this out. :-)
This discussion has been closed.