2017-02-19 8 views
0

複数のチェックボックスに基づいて行を表示/非表示にするHTMLテーブルがあります。複数のチェックボックスを含む行を非表示

(テキスト基準に基づいて)チェックボックスをオンにすると、一部の行が非表示になります。チェックが外されていると、すべての行が表示されます。 2つ以上のチェックボックスがチェックされている場合、すべての選択されたチェックボックスの基準を満たした行を除いて、すべての行が隠れています。しかし、チェクボックスのチェックを外すと、すべての行が表示されます。

私がチェックボックスをオフにすると、現在選択されているすべてのチェックボックスの基準を満たしている行のみを表示するにはどうすればよいですか?

わかりやすくするために、チェックボックスをオフにしたときに、他のチェックボックスで既に非表示になっている行を確認し、表示しないようにする必要があります。作業例

例: CheckBox1をとcheckbox2が選択されています:だけROW1が示されている、私はcheckbox2のチェックを外した場合のみ、ROW1とROW3が示されなければならない

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

答えて

0

あなたがすべてやショー/非表示の行をチェックする関数を作成し、ちょうどチェックボックスchangeイベントでそれを呼び出すことができ、ここでの例です:

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

それを追跡する必要はありません。すべての行を表示し、非表示の基準を処理して一致する行を非表示にします。このような

何か:

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

これは、単一のフレーム上のパフォーマーになりますので、ブラウザがきれいで速い変化を生じる、点滅しません。

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>

このソリューションを見ることができます。

0

目的の動作を実現するには、各要素の実際のステータスを非表示にする必要があります。これは、すべてのチェックボックスに1つのイベントリスナーを使用します。

ロジックは非常に簡単です:次の基準が満たされたときにすべての行を反復処理し、電流を隠す:

はチェックボックスが
  • をチェックされている
    1. 行がまだ
    2. 行が隠されていません

    各隠蔽は状態オブジェクトの更新を伴い、最終的にはすべての行がこの状態オブジェクトには表示されません。

    $(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'); 
         } 
        } 
        }); 
    }); 
    
  • 関連する問題