views:

271

answers:

3

I work for a transit agency and I have large amounts of data (mostly times), and I need a way to filter the data using two textboxes (To and From).

I found jQuery quick search, but it seems to only work with one textbox.

If anyone has any ideas via jQuery or some other client side library, that would be fantastic.

Ideal example:

To: [Textbox] From:[Textbox]

<table>
<tr>
<td>69th street</td><td>5:00pm</td><td>5:06pm</td><td>5:10pm</td><td>5:20pm</td>
</tr>
<tr>
<td>Millbourne</td><td>5:09pm</td><td>5:15pm</td><td>5:20pm</td><td>5:25pm</td>
</tr>
<tr>
<td>Spring Garden</td><td>6:00pm</td><td>6:15pm</td><td>6:20pm</td><td>6:25pm</td>
</tr>
</table>

I have an HTML page with a giant table on it listing the station names and each stations times. I want to be able to put my starting location in one box and my ending location in another box and have all the items in the table disappear that don't relate to either of the two locations typed in, leaving only two rows that match what was typed in (even if they don't spell it right or type it all the way) Similar to the jQuery quick search plugin

+1  A: 

If you want to build your own then here's one possible implementation:

// store values for later
$('#table-to-fiter tr').each(function() {
    var firstCellText = $('td:first', this).text().toLowerCase();
    $(this).data('firstCellText', firstCellText);
});

function refreshFilter() {

    var toValue = $('#to-input').val().toLowerCase(),
        fromValue = $('#from-input').val().toLowerCase(),
        firstCellText;

    $('#table-to-fiter tr').each(function() {
        firstCellText = $(this).data('firstCellText');
        if (firstCellText.indexOf(toValue) !== -1 || firstCellText.indexOf(fromValue) !== -1) {
            $(this).show();
        } else {
            $(this).hide();
        }
    });

}

$('#to-input, #from-input').keyup(refreshFilter);
Ken Browning
I got it to go to the hidden 3rd box, but the issue isn't resolved. If i search 69th street in the first box, and huntington in the second, then the third box reads 69thstreethuntington and there is no value that matches that in the data.
Bry4n
Updated to add a space so that "69th street" and "huntington" result in "69th street huntington". If that's not gonna work for you then you'll need to find another filtering plugin or write your own filtering logic.
Ken Browning
Yeah it seems as if it will take whatever is in the search box literally for each column. That stinks. So I should use this custom method you gave me instead?
Bry4n
I must say though I am not quite sure how to use the other one you gave me.
Bry4n
Can you edit your question so that the desired behavior is clearer? How exactly do you want the filtering to work? Provide examples.
Ken Browning
Not sure what I am doing wrong here...I used your code, made the proper ID adjustments and when i type into the 2nd box, everything disappears.
Bry4n
A: 

It's not fully clear what is your's task is. Lets assume, that you have a set of data, and need to filter it 2 times and display it in a grid.

I would do this in a following way:

  1. To handle data filtering easily, I'll render in into JSON format. Example code will create array of java-script objects with Stantion string field and Time - array of strings. Json representation of data object will allow you to filter by each column, that is key point of representing data in JSON and rendering it on client side.

  2. To do filtering, implement something like Ken proposed - handling on-key-up event of text box, but to take advantage of JSON data representation, filtering function will take StationInfo from data array.

  3. Use JQGrid data to display filtering results.

Rough example:

    var data = [
            {Stantion: '69th street', Time: ['5:00pm', '5:05pm']}, 
            {Stantion: 'Millbourne', Time: ['5:00pm', '5:05pm']} // StationInfo object
    ];
    function RenderData(data)
    {
            // Rendering of data using JQGrid or JTemplates
    }
    $('#textbox-a').keyup(function()
    {
            var textBoxValue = $('#textbox-a').val();
            var filteredItems = $.grep(data, function(a, i)
            {
                    return a.indexOf(textBoxValue) != -1;
            });
            RenderData(filteredItems);
    });
Valera Kolupaev
I'm not sure what part isn't clear. I have an HTML page with a giant table on it listing the station names and each stations times. I want to be able to put my starting location in one box and my ending location in another box and have all the items in the table disappear leaving only two rows that match what was typed in (even if they don't spell it right or type it all the way)Similar to the jQuery quick search plugin
Bry4n
A: 

Do you have access to the data itself? If you do you could create classes and append them to the table rows.

<tr class="Melbourne"><td></td><td></td></tr>

Then, on click of the button do something like

function myFilter(fromloc, toloc) {
  $("tr").hide();
  $("tr." + fromloc).show();
  $("tr." + toloc).show();
}

Would that work? Obviously in practice it may be a bit more complex. But jQuery selectors are incredibly powerful, and I think could handle doing manipulation just like this.

Owen Allen
I guess I could add classes to them, do you have a full code set I could use?
Bry4n
I'm not sure what you mean by full code set? Are you asking for a working example of my solution? If so let me know and I could put a basic one together in a few mins.
Owen Allen
Yes a working example. It would be greatly appreciated. Also after you have the two rows selected will it collapse the other rows as to not leave any space?
Bry4n
Nevermind I figured it out! Thanks.
Bry4n