admin管理员组文章数量:1410725
The select/unselect
button works on the checkbox.
But it does not work for the row table.
//Select row table
$('#example').on('click', 'tr', function() {
var $row = $(this),
isSelected = $row.hasClass('selected')
$row.toggleClass('selected')
.find(':checkbox').prop('checked', !isSelected);
});
// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
var selectAll = this.id === 'selectAll';
$("#example tr :checkbox").prop('checked', selectAll);
});
I think the checklist is just for display, for row selected and to mark it.
How when the select / unselect button is clicked,
it select on row table too, Not just on the checkbox?
You can see if you click the row table.
Code Snippet Demonstration :
$('#example').dataTable();
//Select row table
$('#example').on('click', 'tr', function() {
var $row = $(this),
isSelected = $row.hasClass('selected')
$row.toggleClass('selected')
.find(':checkbox').prop('checked', !isSelected);
});
// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
var selectAll = this.id === 'selectAll';
$("#example tr :checkbox").prop('checked', selectAll);
});
<script src=".12.4.js"></script>
<script src=".10.16/js/jquery.dataTables.min.js"></script>
<link href=".10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
<button type="button" id="selectAll"> Select </button>
<button type="button" id="unselectAll"> UnSelect </button>
<table id="example" class="myclass" />
<thead>
<tr>
<th>
</th>
<th>Name</th>
<th>Company</th>
<th>Employee Type</th>
<th>Address</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" />
</td>
<td>Calvin</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>Ananda</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>John</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>Doe</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
</tbody>
The select/unselect
button works on the checkbox.
But it does not work for the row table.
//Select row table
$('#example').on('click', 'tr', function() {
var $row = $(this),
isSelected = $row.hasClass('selected')
$row.toggleClass('selected')
.find(':checkbox').prop('checked', !isSelected);
});
// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
var selectAll = this.id === 'selectAll';
$("#example tr :checkbox").prop('checked', selectAll);
});
I think the checklist is just for display, for row selected and to mark it.
How when the select / unselect button is clicked,
it select on row table too, Not just on the checkbox?
You can see if you click the row table.
Code Snippet Demonstration :
$('#example').dataTable();
//Select row table
$('#example').on('click', 'tr', function() {
var $row = $(this),
isSelected = $row.hasClass('selected')
$row.toggleClass('selected')
.find(':checkbox').prop('checked', !isSelected);
});
// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
var selectAll = this.id === 'selectAll';
$("#example tr :checkbox").prop('checked', selectAll);
});
<script src="https://code.jquery./jquery-1.12.4.js"></script>
<script src="https://cdn.datatables/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
<button type="button" id="selectAll"> Select </button>
<button type="button" id="unselectAll"> UnSelect </button>
<table id="example" class="myclass" />
<thead>
<tr>
<th>
</th>
<th>Name</th>
<th>Company</th>
<th>Employee Type</th>
<th>Address</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" />
</td>
<td>Calvin</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>Ananda</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>John</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>Doe</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
</tbody>
JSFiddle
Share Improve this question edited Nov 28, 2017 at 2:00 Calvin Ananda asked Nov 28, 2017 at 1:47 Calvin AnandaCalvin Ananda 1,5301 gold badge16 silver badges30 bronze badges2 Answers
Reset to default 6One way to do it is to call .toggleClass()
on the rows providing the second argument that says whether to add or remove the class:
$("#example tr").toggleClass("selected", selectAll)
.find(":checkbox").prop('checked', selectAll);
Added to your demo:
$('#example').dataTable();
//Select row table
$('#example').on('click', 'tr', function() {
var $row = $(this),
isSelected = $row.hasClass('selected')
$row.toggleClass('selected')
.find(':checkbox').prop('checked', !isSelected);
});
// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
var selectAll = this.id === 'selectAll';
$("#example tr").toggleClass("selected", selectAll)
.find(":checkbox").prop('checked', selectAll);
});
<script src="https://code.jquery./jquery-1.12.4.js"></script>
<script src="https://cdn.datatables/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
<button type="button" id="selectAll"> Select </button>
<button type="button" id="unselectAll"> UnSelect </button>
<table id="example" class="myclass" />
<thead>
<tr>
<th></th><th>Name</th><th>Company</th><th>Employee Type</th><th>Address</th><th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" /></td><td>Calvin</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
</tr>
<tr>
<td><input type="checkbox" /></td><td>Ananda</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
</tr>
<tr>
<td><input type="checkbox" /></td><td>John</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
</tr>
<tr>
<td><input type="checkbox" /></td><td>Doe</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
</tr>
</tbody>
Try this code:
$('#example').dataTable();
//Select row table
$('#example').on('click', 'tr', function() {
var $row = $(this);
isSelected = $row.hasClass('selected')
$row.toggleClass('selected').find(':checkbox').prop('checked',!isSelected);
});
//Checkbox !== select row
var isSelectAll = false;
var isUnselectAll = false;
$("#selectAll, #unselectAll").on("click", function() {
var selectAll = this.id === 'selectAll';
if(selectAll){
if(!isSelectAll){
$('#example tr').click();
}
isSelectAll = true;
isUnselectAll = false;
}else{
if(!isUnselectAll){
$('#example tr').click();
}
isSelectAll = false;
isUnselectAll = true;
}
$("#example tr :checkbox").prop('checked', selectAll);
});
本文标签: javascriptSelect CheckboxSelect Row TableStack Overflow
版权声明:本文标题:javascript - Select Checkbox !== Select Row Table - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744812118a2626490.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论