Filter on user input AND defined value

Filter on user input AND defined value

lauralaura Posts: 14Questions: 0Answers: 0
edited May 2010 in General
I've been trying to get this to work for some time with no success.

Each row details a program that is available in multiple cities. One of the columns in the row holds all the city information. However, some of the programs don't have a specific city, as they are available everywhere, so there is a default value of "throughout province".

I would like to allow users to enter the city they are searching and get those results AND all records with the value "throughout".

Is this possible? The following code works for searching on cities. I've trying adding different things to the var iColumn line but was never able to get it to show the city AND records with "throughout".

[code]
function fnFilterLocation ()
{
var jqInputs = $('#filter_location input');
var iColumn = jqInputs[1].value == "" ? null : jqInputs[1].value;
oTable.fnFilter( jqInputs[0].value, 4 );
}
[/code]

Thanks for any help you can provide!!

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Hi laura,

    I think you are looking for OR rather than AND are you not? Either this city OR 'throughout'. Try:

    [code]
    oTable.fnFilter( 'throughout|'+jqInputs[0].value, 4, false );
    [/code]
    So what this is saying is don't escape regular expression characters (the false) and filter for 'throughout' or jqInputs[0].value on column index 4.

    Allan
  • lauralaura Posts: 14Questions: 0Answers: 0
    Hi Allan, thanks for your speedy response WOW - and now that I think about it that way - you're right - I do mean OR and it works perfectly!!!

    Thanks again :)
  • lauralaura Posts: 14Questions: 0Answers: 0
    Hi - I have another question about this type of filtering - Is it possible to perform this fnFilter on two columns?

    Something like this?

    [code]oTable.fnFilter( 'throughout|'+jqInputs[0].value, 4|5, false );[/code]

    That didn't work and I've tried a few other things, but I'm not having any luck

    Thanks!
    Laura
  • lauralaura Posts: 14Questions: 0Answers: 0
    I believe I figured it out ;)

    If anyone else is trying this here is my code on a simple filter:

    [code]oTable.fnFilter( jqInputs[0].value, [3][4] );[/code]
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    I'd be very surprised if that worked! To the point that I'm surprised that doesn't give you a script error! The basic answer is no (assuming it doesn't work), there is no way to just do two given columns using fnFilter (unless you have set bSearchable to false for all other columns) - you would need to use custom filtering, where it is indeed possible.

    Allan
  • lauralaura Posts: 14Questions: 0Answers: 0
    Hmmm.... well it does work - I don't get any scripting errors - here is my entire code (it's doing other things as well):

    [code]
    var oTable;

    function fnFormatDetails ( nTr )
    {
    var aData = oTable.fnGetData( nTr );
    var sOut = '';
    sOut += ' Services'+aData[3]+'';
    sOut += ' Eligibility'+aData[4]+'';
    sOut += ' Locations'+aData[5]+'';
    sOut += ''+aData[6]+'';
    sOut += '';
    return sOut;
    }

    function fnFilterServices ()
    {
    var jqInputs = $('#filter_services input');
    var iColumn = jqInputs[1].value == "" ? null : jqInputs[1].value;
    oTable.fnFilter( jqInputs[0].value, [3][4] );
    }


    function fnFilterLocation ()
    {
    var jqInputs = $('#filter_location input');
    var iColumn = jqInputs[1].value == "" ? null : jqInputs[1].value;
    oTable.fnFilter( 'throughout|'+jqInputs[0].value, 5, false );
    }

    $(document).ready(function() {

    oTable = $('#inventory').dataTable( {
    "aoColumns": [
    { "sWidth": "70px", "bSortable": false},
    { "sWidth": "400px" },
    { "sWidth": "460px", "sType": "html", "bSortable": false },
    { "bVisible": false, "bSortable": false },
    { "bVisible": false, "bSortable": false },
    { "bVisible": false, "bSortable": false },
    { "bVisible": false, "bSortable": false }
    ],
    "aaSorting": [[1, 'asc']],
    "bPaginate": false,
    "bInfo": true,
    "bLengthChange": false,
    "bFilter": false
    });

    $('#filter_services input').keyup( fnFilterServices );
    $('#filter_location input').keyup( fnFilterLocation );

    $(oTable.fnGetNodes() ).each( function () {
    $(this).click( function () {
    var nTr = this;
    if ( $(this).hasClass('row_selected') )
    {
    $(this).removeClass('row_selected');
    oTable.fnClose( nTr );
    }
    else
    {
    $(this).addClass('row_selected');
    oTable.fnOpen( nTr, fnFormatDetails(nTr), 'details' );
    }
    } );
    } );


    } );
    [/code]
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    I think think it's working quite as you expect I'm afraid - if you put [3][4] into the Javascript console of your browser you'll see that it's actually undefined. So what is happening here is that you are passing an undefined value as the second parameter. This results in DataTables just going a global filter - which is matching something and giving the impression that it works! If you change the columns in your "array" to something which shouldn't match, I think it will still match.

    Allan
  • lauralaura Posts: 14Questions: 0Answers: 0
    Oh shoot ;) I see what you're saying now. When i search using that particular input, it is searching the entire table, rather than just those two columns.

    I guess this can't be done the way I have it... I'm stumped :(

    Thanks for pointing that out
This discussion has been closed.