jQuery 使用 jQuery DataTables 获取复选框长度的代码
jQuery code to get checkbox length while using jQuery DataTables
我在使用 jQuery 和 jQuery DataTables 获取复选框长度时得到了错误的值。
HTML:
<table class="table table-bordered" id="dataTables-show-productList">
<thead>
<tr>
<th width="5px"><input type="checkbox" name="Select All" class="chkSelectAll" /></th>
<th>Product Information</th>
</tr>
</thead>
<tbody>
<c:forEach var="masterListVar" items="${masterList}">
<tr>
<td width="1%" align="center">
<c:if test="${masterListVar.saveFlag}">
<input type="checkbox" path="selectChecked" checked class="Projection_test" value="${masterListVar.productId}"/>
</c:if>
<c:if test="${!masterListVar.saveFlag}">
<input type="checkbox" path="selectChecked" class="Projection_test" value="${masterListVar.productId}"/>
</c:if>
</td>
<td>${masterListVar.productInfo}</td>
</tr>
</c:forEach>
</tbody>
</table>
JavaScript:
$('#dataTables-show-productList').DataTable({
width:'100%'
, responsive : true
, "bSort" : false
});
$('.chkSelectAll').click(function () {
$('.Projection_test').prop('checked', $(this).is(':checked'));
});
$('.Projection_test').click(function () {
if ($('.Projection_test:checked').length == $('.Projection_test').length) {
$('.chkSelectAll').prop('checked', true);
}
else {
$('.chkSelectAll').prop('checked', false);
}
});
$('#FavouriteList').click(function (e) {
var selectedRow = $('.Projection_test');
alert($('.Projection_test:checked').length);
e.preventDefault();
});
分页时,同时只选择 12 个值。在警报中它只显示 2 当我保持在第 2 页和测试时。
原因
对于 jQuery DataTables,只有可见行存在于 DOM 中。这就是为什么使用 jQuery $()
方法访问复选框会给你 2 个节点。
解决方案
要 select 复选框,包括 DOM 中不存在的复选框,并考虑帐户当前搜索查询,请使用以下代码:
// Select all available rows with search applied
var rows = $('#dataTables-show-productList').DataTable()
.rows({ 'search': 'applied' })
.nodes();
// Checked checkboxes
console.log($('.Projection_test:checked', rows).length);
// All checkboxes
console.log($('.Projection_test', rows).length);
您需要在所有点击事件处理程序中使用此逻辑:$('.chkSelectAll').click
、$('.Projection_test').click
和 $('#FavouriteList').click
。
注意事项
jQuery DataTables 也有 $()
方法,允许对完整 table 执行 jQuery selection 操作。但是,它不允许过滤掉应用了搜索的行。
请参阅我们的文章 jQuery DataTables – How to add a checkbox column,了解有关如何在使用 jQuery DataTables 时使用复选框的更多信息。
我在使用 jQuery 和 jQuery DataTables 获取复选框长度时得到了错误的值。
HTML:
<table class="table table-bordered" id="dataTables-show-productList">
<thead>
<tr>
<th width="5px"><input type="checkbox" name="Select All" class="chkSelectAll" /></th>
<th>Product Information</th>
</tr>
</thead>
<tbody>
<c:forEach var="masterListVar" items="${masterList}">
<tr>
<td width="1%" align="center">
<c:if test="${masterListVar.saveFlag}">
<input type="checkbox" path="selectChecked" checked class="Projection_test" value="${masterListVar.productId}"/>
</c:if>
<c:if test="${!masterListVar.saveFlag}">
<input type="checkbox" path="selectChecked" class="Projection_test" value="${masterListVar.productId}"/>
</c:if>
</td>
<td>${masterListVar.productInfo}</td>
</tr>
</c:forEach>
</tbody>
</table>
JavaScript:
$('#dataTables-show-productList').DataTable({
width:'100%'
, responsive : true
, "bSort" : false
});
$('.chkSelectAll').click(function () {
$('.Projection_test').prop('checked', $(this).is(':checked'));
});
$('.Projection_test').click(function () {
if ($('.Projection_test:checked').length == $('.Projection_test').length) {
$('.chkSelectAll').prop('checked', true);
}
else {
$('.chkSelectAll').prop('checked', false);
}
});
$('#FavouriteList').click(function (e) {
var selectedRow = $('.Projection_test');
alert($('.Projection_test:checked').length);
e.preventDefault();
});
分页时,同时只选择 12 个值。在警报中它只显示 2 当我保持在第 2 页和测试时。
原因
对于 jQuery DataTables,只有可见行存在于 DOM 中。这就是为什么使用 jQuery $()
方法访问复选框会给你 2 个节点。
解决方案
要 select 复选框,包括 DOM 中不存在的复选框,并考虑帐户当前搜索查询,请使用以下代码:
// Select all available rows with search applied
var rows = $('#dataTables-show-productList').DataTable()
.rows({ 'search': 'applied' })
.nodes();
// Checked checkboxes
console.log($('.Projection_test:checked', rows).length);
// All checkboxes
console.log($('.Projection_test', rows).length);
您需要在所有点击事件处理程序中使用此逻辑:$('.chkSelectAll').click
、$('.Projection_test').click
和 $('#FavouriteList').click
。
注意事项
jQuery DataTables 也有 $()
方法,允许对完整 table 执行 jQuery selection 操作。但是,它不允许过滤掉应用了搜索的行。
请参阅我们的文章 jQuery DataTables – How to add a checkbox column,了解有关如何在使用 jQuery DataTables 时使用复选框的更多信息。