Hide/show(切换)table 单击行
Hide/show (toggle) table rows on click
$(function () {
$( "td" ).on( "click", function() {
var type = $(this).text();
$('td:first-child').parent('tr:not(:contains('+type+'))').toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="vehicles">
<tr>
<th>Type</th>
<th>Color</th>
<th>Wheels</th>
</tr>
<tr>
<td>Car</td>
<td>Red</td>
<td>4</td>
</tr>
<tr>
<td>Motorcycle</td>
<td>Green</td>
<td>2</td>
</tr>
<tr>
<td>Bike</td>
<td>Blue</td>
<td>2</td>
</tr>
<tr>
<td>Car</td>
<td>Blue</td>
<td>4</td>
</tr>
<tr>
<td>Bike</td>
<td>Green</td>
<td>2</td>
</tr>
<tr>
<td>Motorcycle</td>
<td>Red</td>
<td>2</td>
</tr>
</table>
跟进:How to hide/show (toggle) certain table rows on click? 只要 table 元素被切换 on/off,这就有效,但是如果你 select 一行中的一个项目然后 select 另一行中的项目,过滤变得混乱。
可能有更好的方法来执行此操作,但如果您只跟踪最后一次单击的类型并且 table 是否已被过滤,您可以更清楚何时需要重置 table 或应用过滤器。
https://jsfiddle.net/wilchow/stnnuw0a/1/
$(function () {
var last_type = null;
var filtered = false;
$( "td" ).on( "click", function() {
console.log("last_type: "+last_type);
var type = $(this).text();
console.log("type: "+type);
if(filtered && last_type == type) {
// type is already filtered. Show all.
$("tr, td").show();
last_type = null; // reset
filtered = false; // reset
} else {
$("tr, td").show(); // show all before filtering
$('td:first-child').parent('tr:not(:contains('+type+'))').hide();
filtered = true; // set filter flag to true
}
last_type = type; // set last type clicked
});
});
$(function () {
$( "td" ).on( "click", function() {
var type = $(this).text();
$('td:first-child').parent('tr:not(:contains('+type+'))').toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="vehicles">
<tr>
<th>Type</th>
<th>Color</th>
<th>Wheels</th>
</tr>
<tr>
<td>Car</td>
<td>Red</td>
<td>4</td>
</tr>
<tr>
<td>Motorcycle</td>
<td>Green</td>
<td>2</td>
</tr>
<tr>
<td>Bike</td>
<td>Blue</td>
<td>2</td>
</tr>
<tr>
<td>Car</td>
<td>Blue</td>
<td>4</td>
</tr>
<tr>
<td>Bike</td>
<td>Green</td>
<td>2</td>
</tr>
<tr>
<td>Motorcycle</td>
<td>Red</td>
<td>2</td>
</tr>
</table>
跟进:How to hide/show (toggle) certain table rows on click? 只要 table 元素被切换 on/off,这就有效,但是如果你 select 一行中的一个项目然后 select 另一行中的项目,过滤变得混乱。
可能有更好的方法来执行此操作,但如果您只跟踪最后一次单击的类型并且 table 是否已被过滤,您可以更清楚何时需要重置 table 或应用过滤器。
https://jsfiddle.net/wilchow/stnnuw0a/1/
$(function () {
var last_type = null;
var filtered = false;
$( "td" ).on( "click", function() {
console.log("last_type: "+last_type);
var type = $(this).text();
console.log("type: "+type);
if(filtered && last_type == type) {
// type is already filtered. Show all.
$("tr, td").show();
last_type = null; // reset
filtered = false; // reset
} else {
$("tr, td").show(); // show all before filtering
$('td:first-child').parent('tr:not(:contains('+type+'))').hide();
filtered = true; // set filter flag to true
}
last_type = type; // set last type clicked
});
});