2017-02-19 1 views
0

J'ai un tableau HTML dans lequel je veux masquer/afficher les lignes en fonction de plusieurs cases à cocher.Masquer les lignes avec plusieurs cases à cocher

Lorsqu'une case est cochée, (en fonction d'un critère de texte), certaines lignes se cachent, si elle n'est pas cochée, toutes les lignes sont affichées. Si 2 cases ou plus sont cochées, toutes les lignes sont cachées, sauf celles qui répondent aux critères de toutes les cases cochées. Mais si je désélectionne l'une des checbox, toutes les lignes sont affichées.

Ma question est la suivante: lorsque je désélectionne l'une des cases à cocher, comment puis-je afficher UNIQUEMENT les lignes répondant aux critères de toutes les cases sélectionnées?

Pour mieux comprendre, je dois vérifier quelles lignes sont déjà masquées par les autres cases et ne pas les afficher lorsqu'une case n'est pas cochée.

Exemple de cas de travail: checkbox1 et checkbox2 sont sélectionnés: seulement row1 est affiché, si je décocher checkbox2, ne row1 et row3 doivent être présentés

HTML:

<label><input type="checkbox" id="checkbox1">Teacher</label> 
<label><input type="checkbox" id="checkbox2">Tennis</label> 
<label><input type="checkbox" id="checkbox3">Married</label> 

    <table id="table" border="1"> 
    <thead> 
     <th>First Name</th> 
     <th>Last Name</th> 
     <th>Profession</th> 
     <th>Hobby</th> 
     <th>Married</th> 
    </thead> 
    <tbody> 
    <tr id="row1"> 
     <td>John</td> 
     <td>Doe</td> 
     <td>Teacher</td> 
     <td>Tennis</td> 
     <td>Yes</td> 
    </tr> 
    <tr id="row2"> 
     <td>Eve</td> 
     <td>Jackson</td> 
     <td>Doctor</td> 
     <td>Darts</td> 
     <td>No</td> 
    </tr> 
    <tr id="row3"> 
     <td>Adam</td> 
     <td>Johnson</td> 
     <td>Teacher</td> 
     <td>Skydiving</td> 
     <td>Yes</td> 
    </tr> 
    <tr id="row4"> 
     <td>Nina</td> 
     <td>Pursuit</td> 
     <td>Lawyer</td> 
     <td>Tennis</td> 
     <td>Yes</td> 
    </tr> 
    </tbody> 
    </table> 

jQuery:

$(document).ready(function() { 
    $('#checkbox1').change(function() { 
      for (var i = 0; i < 5; i++) { 
       if ((this.checked) && $("#table #row"+i+" td:nth-child(3):not(:contains('Teacher'))").length){ 
        $('#row'+i).fadeOut('slow'); 
       } 
       if (!this.checked) $('#row'+i).fadeIn('slow'); 
       } 
    }); 

    $('#checkbox2').change(function() { 
      for (var i = 0; i < 5; i++) { 
       if ((this.checked) && $("#table #row"+i+" td:nth-child(4):not(:contains('Tennis'))").length){ 
        $('#row'+i).fadeOut('slow'); 
       } 
       if (!this.checked) $('#row'+i).fadeIn('slow'); 
       } 
    }); 

    $('#checkbox3').change(function() { 
      for (var i = 0; i < 5; i++) { 
       if ((this.checked) && $("#table #row"+i+" td:nth-child(5):not(:contains('Yes'))").length){ 
        $('#row'+i).fadeOut('slow'); 
       } 
       if (!this.checked) $('#row'+i).fadeIn('slow'); 
       } 
    }); 
}); 

JSFiddle DEMO

Répondre

0

Vous pouvez créer une fonction qui vérifie tous et spectacles/lignes cacher, et il suffit d'appeler sur événement case change est ici un exemple:

function show_hide_rows() { 
 
\t var checkbox1=($('#checkbox1').is(':checked') ? true : false), 
 
\t \t checkbox2=($('#checkbox2').is(':checked') ? true : false), 
 
\t \t checkbox3=($('#checkbox3').is(':checked') ? true : false); 
 
\t 
 
\t var passed; 
 
\t for (var i = 0; i < 5; i++) { 
 
\t \t passed = false; 
 
\t \t if (checkbox1 && $("#table #row"+i+" td:nth-child(3):not(:contains('Teacher'))").length) passed = true; 
 
\t \t if (checkbox2 && $("#table #row"+i+" td:nth-child(4):not(:contains('Tennis'))").length) passed = true; 
 
\t \t if (checkbox3 && $("#table #row"+i+" td:nth-child(5):not(:contains('Yes'))").length) passed = true; 
 
\t \t 
 
\t \t if (passed) $('#row'+i).fadeOut('slow'); 
 
\t \t else $('#row'+i).fadeIn('slow'); 
 
\t \t } 
 
} 
 

 
$(function(){ 
 
    $('input[type="checkbox"]').on('change', function(){ show_hide_rows(); }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<label><input type="checkbox" id="checkbox1">Teacher</label> 
 
<label><input type="checkbox" id="checkbox2">Tennis</label> 
 
<label><input type="checkbox" id="checkbox3">Married</label> 
 

 
    <table id="table" border="1"> 
 
    <thead> 
 
     <th>First Name</th> 
 
     <th>Last Name</th> 
 
     <th>Profession</th> 
 
     <th>Hobby</th> 
 
     <th>Married</th> 
 
    </thead> 
 
    <tbody> 
 
    <tr id="row1"> 
 
     <td>John</td> 
 
     <td>Doe</td> 
 
     <td>Teacher</td> 
 
     <td>Tennis</td> 
 
     <td>Yes</td> 
 
    </tr> 
 
    <tr id="row2"> 
 
     <td>Eve</td> 
 
     <td>Jackson</td> 
 
     <td>Doctor</td> 
 
     <td>Darts</td> 
 
     <td>No</td> 
 
    </tr> 
 
    <tr id="row3"> 
 
     <td>Adam</td> 
 
     <td>Johnson</td> 
 
     <td>Teacher</td> 
 
     <td>Skydiving</td> 
 
     <td>Yes</td> 
 
    </tr> 
 
    <tr id="row4"> 
 
     <td>Nina</td> 
 
     <td>Pursuit</td> 
 
     <td>Lawyer</td> 
 
     <td>Tennis</td> 
 
     <td>Yes</td> 
 
    </tr> 
 
    </tbody> 
 
    </table>

0

Vous n'avez pas besoin de suivre cela. Affichez simplement toutes les lignes et traitez les critères de masquage pour masquer les lignes correspondantes.

Quelque chose comme ceci:

When a checkbox is changed 
Show all rows 
Check all checkboxes, and hide those rows that match the criteria 

Puisque ce sera interprète sur un cadre unique, le navigateur ne clignote, ce qui entraîne un changement rapide et propre.

0

$(document).ready(function() { 
 
    $('#checkbox1').change(function() { 
 
    for (var i = 0; i < 5; i++) { 
 
     if ((this.checked) && $("#table #row" + i + " td:nth-child(3):not(:contains('Teacher'))").length) { 
 
     // \t \t \t \t $('#row'+i).fadeOut('slow'); 
 
     $('#table #row' + i).addClass('check1'); 
 
     } 
 
     if (!this.checked) $('#table #row' + i).removeClass('check1'); 
 
    } 
 
    }); 
 

 
    $('#checkbox2').change(function() { 
 
    for (var i = 0; i < 5; i++) { 
 
     if ((this.checked) && $("#table #row" + i + " td:nth-child(4):not(:contains('Tennis'))").length) { 
 
     // \t \t \t \t \t $('#row'+i).fadeOut('slow'); 
 
     $('#table #row' + i).addClass('check2'); 
 
     } 
 
     if (!this.checked) $('#table #row' + i).removeClass('check2'); 
 
    } 
 
    }); 
 

 
    $('#checkbox3').change(function() { 
 
    for (var i = 0; i < 5; i++) { 
 
     if ((this.checked) && $("#table #row" + i + " td:nth-child(5):not(:contains('Yes'))").length) { 
 
     //$('#row'+i).fadeOut('slow'); 
 
     $('#table #row' + i).addClass('check3'); 
 
     } 
 
     if (!this.checked) $('#table #row' + i).removeClass('check3'); 
 
    } 
 
    }); 
 
    $('.checkbox').change(function() { 
 
    for (var i = 0; i < 5; i++) { 
 
     if ($("#table #row" + i).hasClass('check1') || $("#table #row" + i).hasClass('check2') || $("#table #row" + i).hasClass('check3')) { 
 
     $('#row' + i).fadeOut('slow'); 
 
     // $('#table #row'+i).addClass('check3'); 
 
     } else { 
 
     $('#row' + i).fadeIn('slow'); 
 
     } 
 

 
    } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<label> 
 
    <input type="checkbox" class="checkbox" id="checkbox1">Teacher</label> 
 
<label> 
 
    <input type="checkbox" class="checkbox" id="checkbox2">Tennis</label> 
 
<label> 
 
    <input type="checkbox" class="checkbox" id="checkbox3">Married</label> 
 

 
<table id="table" border="1"> 
 
    <thead> 
 
    <th>First Name</th> 
 
    <th>Last Name</th> 
 
    <th>Profession</th> 
 
    <th>Hobby</th> 
 
    <th>Married</th> 
 
    </thead> 
 
    <tbody> 
 
    <tr id="row1"> 
 
     <td>John</td> 
 
     <td>Doe</td> 
 
     <td>Teacher</td> 
 
     <td>Tennis</td> 
 
     <td>Yes</td> 
 
    </tr> 
 
    <tr id="row2"> 
 
     <td>Eve</td> 
 
     <td>Jackson</td> 
 
     <td>Doctor</td> 
 
     <td>Darts</td> 
 
     <td>No</td> 
 
    </tr> 
 
    <tr id="row3"> 
 
     <td>Adam</td> 
 
     <td>Johnson</td> 
 
     <td>Teacher</td> 
 
     <td>Skydiving</td> 
 
     <td>Yes</td> 
 
    </tr> 
 
    <tr id="row4"> 
 
     <td>Nina</td> 
 
     <td>Pursuit</td> 
 
     <td>Lawyer</td> 
 
     <td>Tennis</td> 
 
     <td>Yes</td> 
 
    </tr> 
 
    </tbody> 
 
</table>

Vous pouvez rechercher cette solution.

0

Pour atteindre votre comportement souhaité, vous devez stocker l'état réel de chaque élément lorsque celui-ci est caché. Cela utilise un écouteur d'événement pour toutes les cases à cocher.

La logique est assez simple: itérer sur toutes les lignes et cacher le courant lorsque les critères suivants sont réunis:

  1. La case est cochée
  2. La ligne n'est pas caché encore
  3. La ligne contient l'élément spécifique

Chaque masquage implique une mise à jour de l'objet d'état et, à la fin, toutes les lignes sont affichées et ne figurent pas dans cet objet d'état.

$(document).ready(function() { 
    $('input[type="checkbox"]').click(function() { 

    var checkbox_1 = $('#checkbox1')[0].checked 
    var checkbox_2 = $('#checkbox2')[0].checked 
    var checkbox_3 = $('#checkbox3')[0].checked 
    var state = {} 

    for (var i = 0; i < 5; i++) { 
     if (checkbox_1 && !state[i] && $("#table #row"+i+" td:nth-child(3):not(:contains('Teacher'))").length){ 
     $('#row'+i).fadeOut('slow'); 
     state[i] = "hidden" 
     } 
     if (checkbox_2 && !state[i] && $("#table #row"+i+" td:nth-child(4):not(:contains('Tennis'))").length){ 
     $('#row'+i).fadeOut('slow'); 
     state[i] = "hidden" 
     } 
     if (checkbox_3 && !state[i] && $("#table #row"+i+" td:nth-child(5):not(:contains('Yes'))").length){ 
     $('#row'+i).fadeOut('slow'); 
     state[i] = "hidden" 
     } 
     if (!state[i]) { 
     $('#row'+i).fadeIn('slow'); 
     } 
    } 
    }); 
});