如何单击在 table 中显示 2 个特定列的一个复选框
How to click one checkbox that shows 2 specific columns in a table
我有一个 table 链接到我的 SQL。我想使用单击时仅显示 table.
中的 2 个特定列的复选框
示例:单击工资复选框时,应显示 salary_column 和 extension_column。
我只让它在只有一列的情况下显示
我试过以下方法:
$(function(){
$(':checkbox').on('change', function(){
$('th, td', 'tr').filter(':nth-child(' + $(this).data('col') + ')').toggle();
$('td:nth-child(2),th:nth-child(2)').show();
$('td:nth-child(3),th:nth-child(3)').show();
});
});
<input type="checkbox" data-col="2" class="example" checked="false" /> Salary
<input type="checkbox" data-col="3" class="example" checked="false" /> Position
<input type="checkbox" data-col="4" class="example" checked="false" /> City
<input type="checkbox" data-col="5" class="example" checked="false" /> Ext
<input type="checkbox" data-col="6" class="example" checked="false" /> Joined Date
<input type="checkbox" data-col="7" class="example" checked="false" /> Age
<input id="btnHide" onclick="uncheckAll2()" type="button" value="CEAR ALL"/>
<table id="employee-grid" class="w3-table-all cellspacing="0" width="100%">
<thead>
<tr>
<th class="employee_name" >Name</th>
<th class="employee_salary" >Salary</th>
<th class="employee_position">Position</th>
<th class="employee_city">City</th>
<th class="employee_extension">Ext</th>
<th class="employee_joining_date">Joined</th>
<th class="employee_age">Age</th>
</tr>
</thead>
<tbody>
<?php
$db = new PDO("mysql:host=localhost;dbname=test","root","");
$stmt = $db->prepare("select * from employee");
$stmt->execute();
while($row = $stmt->fetch()){
?>
<tr>
<td><?php echo $row['employee_name'] ?> </td>
<td id="sal"><?php echo $row['employee_salary'] ?> </td>
<td id="pos"><?php echo $row['employee_position'] ?></td>
<td id="cit"><?php echo $row['employee_city'] ?></td>
<td id="exts"><?php echo $row['employee_extension'] ?></td>
<td id="jdat"><?php echo $row['employee_joining_date'] ?></td>
<td id="agi"><?php echo $row['employee_age'] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<script>
$(function(){
$(':checkbox').on('change', function(){
$('th, td', 'tr').filter(':nth-child(' + $(this).data('col') + ')').toggle();
});
});
</script>
试着做成这样:
我的想法是...每个复选框都引用了 <th>
(指向 show/hide 的列)。我使用 data-target
和 jquery 选择器作为值。
<input type="checkbox" data-target=".employee_salary, .employee_extension" checked /> Salary
<input type="checkbox" data-target=".employee_position" checked /> Position
<input type="checkbox" data-target=".employee_city" checked /> City
<input type="checkbox" data-target=".employee_joining_date" checked /> Joined Date
<input type="checkbox" data-target=".employee_age" checked /> Age
通过使用jQuery,得到每个<th>
的n-th位置。之后,遍历所有行和 show/hide n-th 列。
$(function() {
$(':checkbox').on('change', function() {
var $checkbox = $(this);
var state = $checkbox.prop('checked'); // true -> show, false -> hide
// iterate all <th> referenced by <input data-target="">
$($checkbox.data('target')).each(function() {
var index = $(this).index(); // get its n-th position
// iterate all rows, show/hide the n-th column
$('table tr').each(function() {
if (state) {
$(this).find('th:eq(' + index + ')').show();
$(this).find('td:eq(' + index + ')').show();
} else {
$(this).find('th:eq(' + index + ')').hide();
$(this).find('td:eq(' + index + ')').hide();
}
});
});
});
});
我有一个 table 链接到我的 SQL。我想使用单击时仅显示 table.
中的 2 个特定列的复选框示例:单击工资复选框时,应显示 salary_column 和 extension_column。
我只让它在只有一列的情况下显示
我试过以下方法:
$(function(){
$(':checkbox').on('change', function(){
$('th, td', 'tr').filter(':nth-child(' + $(this).data('col') + ')').toggle();
$('td:nth-child(2),th:nth-child(2)').show();
$('td:nth-child(3),th:nth-child(3)').show();
});
});
<input type="checkbox" data-col="2" class="example" checked="false" /> Salary
<input type="checkbox" data-col="3" class="example" checked="false" /> Position
<input type="checkbox" data-col="4" class="example" checked="false" /> City
<input type="checkbox" data-col="5" class="example" checked="false" /> Ext
<input type="checkbox" data-col="6" class="example" checked="false" /> Joined Date
<input type="checkbox" data-col="7" class="example" checked="false" /> Age
<input id="btnHide" onclick="uncheckAll2()" type="button" value="CEAR ALL"/>
<table id="employee-grid" class="w3-table-all cellspacing="0" width="100%">
<thead>
<tr>
<th class="employee_name" >Name</th>
<th class="employee_salary" >Salary</th>
<th class="employee_position">Position</th>
<th class="employee_city">City</th>
<th class="employee_extension">Ext</th>
<th class="employee_joining_date">Joined</th>
<th class="employee_age">Age</th>
</tr>
</thead>
<tbody>
<?php
$db = new PDO("mysql:host=localhost;dbname=test","root","");
$stmt = $db->prepare("select * from employee");
$stmt->execute();
while($row = $stmt->fetch()){
?>
<tr>
<td><?php echo $row['employee_name'] ?> </td>
<td id="sal"><?php echo $row['employee_salary'] ?> </td>
<td id="pos"><?php echo $row['employee_position'] ?></td>
<td id="cit"><?php echo $row['employee_city'] ?></td>
<td id="exts"><?php echo $row['employee_extension'] ?></td>
<td id="jdat"><?php echo $row['employee_joining_date'] ?></td>
<td id="agi"><?php echo $row['employee_age'] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<script>
$(function(){
$(':checkbox').on('change', function(){
$('th, td', 'tr').filter(':nth-child(' + $(this).data('col') + ')').toggle();
});
});
</script>
试着做成这样:
我的想法是...每个复选框都引用了 <th>
(指向 show/hide 的列)。我使用 data-target
和 jquery 选择器作为值。
<input type="checkbox" data-target=".employee_salary, .employee_extension" checked /> Salary
<input type="checkbox" data-target=".employee_position" checked /> Position
<input type="checkbox" data-target=".employee_city" checked /> City
<input type="checkbox" data-target=".employee_joining_date" checked /> Joined Date
<input type="checkbox" data-target=".employee_age" checked /> Age
通过使用jQuery,得到每个<th>
的n-th位置。之后,遍历所有行和 show/hide n-th 列。
$(function() {
$(':checkbox').on('change', function() {
var $checkbox = $(this);
var state = $checkbox.prop('checked'); // true -> show, false -> hide
// iterate all <th> referenced by <input data-target="">
$($checkbox.data('target')).each(function() {
var index = $(this).index(); // get its n-th position
// iterate all rows, show/hide the n-th column
$('table tr').each(function() {
if (state) {
$(this).find('th:eq(' + index + ')').show();
$(this).find('td:eq(' + index + ')').show();
} else {
$(this).find('th:eq(' + index + ')').hide();
$(this).find('td:eq(' + index + ')').hide();
}
});
});
});
});