plugs
plugs

Reputation: 11

Filter HTML Table with jQuery

I am creating a comparison table about broadband and wish to add some jQuery UI Sliders to the top of the table that will enable you to filter the data based on Download Usage, Speed etc.

For example if I slide the slider to 20GB only the rows that have a download usage of 20GB and over will be shown. Is this possible?

I would like to add this functionality to sort the table: http://jqueryui.com/demos/slider/#rangemin

Or if that is not possible then a drop down would be fine. If I have to pull the data in via ajax then that would be fine too.

Here is my code for the table with 1 row.

<table>

<thead>
    <tr>
        <th class="bestseller"></th>
        <th class="device"></th>
        <th class="logo"></th>
        <th class="package">Bestsellers</th>
        <th class="speed">Speed</th>
        <th class="data">Data</th>
        <th class="term">Term</th>
        <th class="price">Price</th>
        <th class="button"></th>
    </tr>
</thead>

<tbody>

    <tr>
        <td class="bestseller">1</td><td class="device"><img alt="Dongle" class="dongle" src="images/dongles/three.png"></td>
        <td class="logo"><img alt="Logo" src="images/three.png"></td>
        <td class="package"><div class="name">Three Standard Broadband</div><div class="info">Great deal including a free dongle.</div></td>
        <td class="speed"><div class="upto">up to</div>7.2Mbps</td>
        <td class="data">15GB</td>
        <td class="term">24<div class="months">Month(s)</div></td>
        <td class="price">£15.99</td>
        <td class="button"><div class="deal">See Deal</div></td>
    </tr>
</tbody>

</table>

Upvotes: 0

Views: 17643

Answers (2)

sina
sina

Reputation: 1

$(document).ready(function(){
    $("#input").on("keyup",function(){
        value = $(this).val().toLowerCase();
        $("#table tr").filter(function(){
            $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
        })

    })
}) 

Upvotes: 0

NibblyPig
NibblyPig

Reputation: 52932

Here is a working example: http://jsfiddle.net/EKpGk/

<table>
<thead>
    <tr>
        <th>Name</th>
        <th>Price</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Super Phone</td>
        <td class="price">£15.99</td>
    </tr>
    <tr>
        <td>Wonder Phone</td>
        <td class="price">£25.99</td>
    </tr>
</tbody>
</table>

<br/>

Filter - enter minimum price:

<input type='text' id='filter' />

<button id='btnFilter'>Go</button>

Javascript (Requires JQuery)

$('#btnFilter').click(function() {

    var price = $('#filter').val();

    $('tr').show();

    $('tr td.price').each(function() {
        if ($(this).text().substring(1) < price)
        {
            $(this).parent().hide();
        }
    });

});

This code is a basic way of filtering it. The substring(1) removes the £ from the price. It hides all rows that have a price less than what you enter. I hope you can adapt it to solve your problem:)

Upvotes: 3

Related Questions