Dynamic filter with jQuery

So, I am working with PHP, SQL and other external languages. A client of mine wants a dynamic filter on his site. I found the script on the internet, but it doesn't work.

$(document).ready(function() {
  $('.results > td').hide();

  $('div.tags').find('input:checkbox').on('click', function() {
    $('.results > td').hide();
    $('div.tags').find('input:checked').each(function() {
      $('.results > td.' + $(this).attr('rel')).show();
    });
  });
});
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box col-md-12 items tags">
  <ul>
    <li class="head">Producent</li>
    <li>
      <input id="producent1" type="checkbox" class="szukajnapraw Promise" rel="Promise" />Promise</li>
    <li>
      <input id="producent2" type="checkbox" class="szukajnapraw NetStor" rel="NetStor" />NetStor</li>
    <li>
      <input id="producent3" type="checkbox" class="szukajnapraw Stardom" rel="Stardom" />Stardom</li>
    <li>
      <input id="producent4" type="checkbox" class="szukajnapraw LaCie" rel="LaCie" />LaCie</li>
    <li>
      <input id="producent5" type="checkbox" class="szukajnapraw G-Tech" rel="G-Tech" />G-Tech</li>
    <li>
      <input id="producent6" type="checkbox" class="szukajnapraw mLogic" rel="mLogic" />mLogic</li>
    <li>
      <input id="producent7" type="checkbox" class="szukajnapraw Tiger Technology" rel="Tiger Technology" />Tiger Technology</li>
  </ul>
</div>
      

Run code


Table:

<table class="table table-striped table-bordered">
            <thead>
                <tr>
                <th>Producent</th>
                <th>Produkt</th>
                <th>Foto</th>
                <th>Typ</th>
                <th>Cena netto</th>
                <th>Cena brutto</th>
                <th>Interface</th>
                <th>Ilość dysków</th>
                <th>Pojemność</th>
                <th>RAID</th>
                <th>Wydajność</th>
                <th>Opis</th>
                <th>Specyfikacja</th>
                <th>Zakup</th>
                </tr>
            </thead>
 
            <tbody class="results">
            <tr> <td>Tiger Technology</td><td>Macierz 123 TEST</td><td><img src="admin/forms/temp/ff.jpg" style = "width:50px;height:50px;"/></td><td>NAS Network Attached Storage</td><td>69.00</td><td>124.00</td><td>Thunderbolt2</td><td>60</td><td>5tb</td><td>0 1</td><td>Duża</td><td>dsadsadasdawderwasfcagfae</td><td><a href=http://www.google.pl target='_blank'>Specyfikacja producenta</a></td><td><form action='' method='POST'>
    <input type='hidden' name='id' value='30' />
    <input class='btn btn-primary btn-sm' type='submit' name='koszykAdd' value='Dodaj do koszyka'/></form></td></tr><tr> <td>Stardom</td><td>dsa</td><td><img src="admin/forms/temp/" style = "width:50px;height:50px;"/></td><td></td><td>1.00</td><td>1.00</td><td></td><td>4</td><td></td><td></td><td></td><td></td><td><a href=http://www.facebook.com target='_blank'>Specyfikacja producenta</a></td><td><form action='' method='POST'>
    <input type='hidden' name='id' value='31' />
    <input class='btn btn-primary btn-sm' type='submit' name='koszykAdd' value='Dodaj do koszyka'/></form></td></tr><tr> <td>G-Tech</td><td>vcxv</td><td><img src="admin/forms/temp/" style = "width:50px;height:50px;"/></td><td>DAS Direct Attached Storage</td><td>50.00</td><td>70.00</td><td>USB 3.0</td><td>5</td><td>400</td><td>5*5*</td><td>500</td><td>dsadsad</td><td><a href=http://www.o2.pl target='_blank'>Specyfikacja producenta</a></td><td><form action='' method='POST'>
    <input type='hidden' name='id' value='34' />
    <input class='btn btn-primary btn-sm' type='submit' name='koszykAdd' value='Dodaj do koszyka'/></form></td></tr><tr> <td>Tiger Technology</td><td>das sad</td><td><img src="admin/forms/temp/IMG_20150504_074450.jpg" style = "width:50px;height:50px;"/></td><td>SAN Srorage Area Network</td><td>50.00</td><td>70.00</td><td>eSATA</td><td>6</td><td>400</td><td>5/8/8</td><td>500</td><td>dasdasdsa</td><td><a href=http://www.nw.pl target='_blank'>Specyfikacja producenta</a></td><td><form action='' method='POST'>
    <input type='hidden' name='id' value='35' />
    <input class='btn btn-primary btn-sm' type='submit' name='koszykAdd' value='Dodaj do koszyka'/></form></td></tr><tr> <td>LaCie</td><td>Macierz</td><td><img src="admin/forms/temp/depositphotos_12071772-Website-Development-PHP-HTML-Arrows.jpg" style = "width:50px;height:50px;"/></td><td>SAN Srorage Area Network</td><td>98.00</td><td>120.00</td><td>miniSAS</td><td>6</td><td>4 TB</td><td>5</td><td>Duża</td><td>Tu powinien być opis, ale testuje i go nie wpisuje! </td><td><a href=http://www.google.pl target='_blank'>Specyfikacja producenta</a></td><td><form action='' method='POST'>
    <input type='hidden' name='id' value='36' />
    <input class='btn btn-primary btn-sm' type='submit' name='koszykAdd' value='Dodaj do koszyka'/></form></td></tr><tr> <td>LaCie</td><td>dsad</td><td><img src="admin/forms/temp/IMG_20150504_074450.jpg" style = "width:50px;height:50px;"/></td><td>SAN Srorage Area Network</td><td>50.00</td><td>70.00</td><td>Thunderbolt</td><td>8</td><td>400</td><td>8/8/8</td><td>500</td><td>fasdf</td><td><a href=http://fsdfsdf target='_blank'>Specyfikacja producenta</a></td><td><form action='' method='POST'>
    <input type='hidden' name='id' value='37' />
    <input class='btn btn-primary btn-sm' type='submit' name='koszykAdd' value='Dodaj do koszyka'/></form></td></tr><tr> <td>mLogic</td><td>test</td><td><img src="admin/forms/temp/ff.jpg" style = "width:50px;height:50px;"/></td><td>DAS Direct Attached Storage</td><td>80.00</td><td>100.00</td><td>Thunderbolt</td><td>8</td><td>900</td><td>5/8/8</td><td>600</td><td>fsdfd</td><td><a href=http://fsdfds target='_blank'>Specyfikacja producenta</a></td><td><form action='' method='POST'>
    <input type='hidden' name='id' value='38' />
    <input class='btn btn-primary btn-sm' type='submit' name='koszykAdd' value='Dodaj do koszyka'/></form></td></tr><tr> <td>Promise</td><td>Zdjęcie</td><td><img src="admin/forms/temp/depositphotos_12071772-Website-Development-PHP-HTML-Arrows.jpg" style = "width:50px;height:50px;"/></td><td>DAS Direct Attached Storage</td><td>4564.00</td><td>999999.99</td><td>Thunderbolt</td><td>4</td><td>5</td><td>1</td><td>300</td><td>SPrawdzam czy działaja fotki .... .... .. .. .. .</td><td><a href=http://feasd target='_blank'>Specyfikacja producenta</a></td><td><form action='' method='POST'>
    <input type='hidden' name='id' value='39' />
    <input class='btn btn-primary btn-sm' type='submit' name='koszykAdd' value='Dodaj do koszyka'/></form></td></tr>            </tbody>
            </table> 
      

Run code


If a checkbox is checked, I want to show its rel or value. I also need to show only the table cells where "Producent" is rel.

How can i do this?

+3


source to share


1 answer


You need to check the content of the first td, not the class

$(document).ready(function () {
    $('.results > tr').hide();

    $('div.tags').find('input:checkbox').on('click', function () {
        $('.results > tr').hide();
        $('div.tags').find('input:checked').each(function () {
            $('.results > tr').has('td:first-child:contains("' + $(this).attr('rel')+'")').show();
        });
    });
});

      

Demo: Fiddle




But the best solution would be to change the markup so that the tr element has a value as an attribute value, for example <tr data-producent="G-Tech">

then

$(document).ready(function () {
    $('.results > tr').hide();

    $('div.tags').find('input:checkbox').on('click', function () {
        $('.results > tr').hide();
        $('div.tags').find('input:checked').each(function () {
            $('.results > tr[data-producent="' + $(this).attr('rel') + '"]').show();
        });
    });
});

      

Demo: Fiddle

+6


source







All Articles