2017-02-19 113 views
0

我有一個HTML表格,我想在其中隱藏/顯示基於多個複選框的行。隱藏有多個複選框的行

當一個複選框被選中時,(根據文本標準)一些行被隱藏,如果未選中,則顯示所有行。如果選中了2個或更多複選框,則所有行都是隱藏的,除了符合所有選定複選框的條件的行之外。但是如果我取消選中任何一個checboxes,所有的行都會顯示出來。

我的問題是,當我取消選中其中一個複選框時,如何僅顯示符合所有當前選定複選框的條件的行?

爲了更好的理解,我需要檢查哪些行已被其他複選框隱藏,並且在複選框未選中時不顯示它們。工作箱子

實施例: 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. 複選框被選中
  2. 該行沒有隱藏尚未
  3. 行確實包含特定元素

每個隱藏都涉及到狀態對象的更新,最後顯示的所有行都不在此狀態對象中。

$(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'); 
     } 
    } 
    }); 
});