javascript - 如果有类活动 jQuery,如何删除 DataTable 中的完整行
问题描述
我使用数据表,我需要隐藏我上课的所有行.fa-active
<em class="fa fa-active" data-title="Active"></em>
我尝试使用这样的代码:
$('tr').each(function () {
$(this).find('.fa-active').remove();
});
但是这段代码只隐藏了行内的图标,我怎样才能完全隐藏带有这样的元素的行?
<table class="custom-style" id="systemRfidCardsSearchTable">
<thead class="table-head">
<tr>
<th class="checkbox-mark sorting_disabled">
<input type="checkbox" class="custom-checkbox" id="checkAll" />
</th>
<th class="sorting_disabled"></th>
<th>@Localizer["Card name"]</th>
<th>@Localizer["RFID code"]</th>
<th class="sorting_disabled"></th>
</tr>
</thead>
<tbody class="table-body">
@foreach (var card in Model.SystemRfidCards)
{
<tr>
<td>
<input type="checkbox" class="custom-checkbox checkbox-mark" id="checkbox_@card.RfidCardId" />
</td>
<td>
@if (card.IsActive)
{
<em class="fa fa-check pointer fa-active" data-title="@SharedLocalizer["Active"]"></em>
}
else
{
<em class="fa fa-close pointer fa-inactive" data-title="@SharedLocalizer["Inactive"]"></em>
}
</td>
<td>@card.RfidCardName</td>
<td>@card.RfidCardCode</td>
</tr>
}
</tbody>
</table>
解决方案
您可以使用$.fn.dataTable.ext.search..
然后在其中添加您的自定义过滤器,即:您需要隐藏的行。所以,如果 tr 有元素,你需要隐藏它,.fa-active
你可以使用$(table.row(dataIndex).node()).find('.fa-active').length == 0
这将返回所有没有该元素的 trs 并显示它们。
演示代码:
$(document).ready(function() {
var table = $('#systemRfidCardsSearchTable').DataTable();
//on click of hide
$("#hide").click(function() {
$.fn.dataTable.ext.search.push(
function(settings, data, dataIndex) {
//find fa-active if length is > 0
return $(table.row(dataIndex).node()).find('.fa-active').length == 0;
}
);
table.draw();
});
$("#reset").click(function() {
//reset your changes..
$.fn.dataTable.ext.search.pop();
table.draw();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.0/css/jquery.dataTables.css" />
<script type="text/javascript" src="http://cdn.datatables.net/1.10.0/js/jquery.dataTables.js"></script>
<button id="hide">Hide</button>
<button id="reset">Reset</button>
<table class="custom-style" id="systemRfidCardsSearchTable">
<thead class="table-head">
<tr>
<th class="checkbox-mark sorting_disabled">
<input type="checkbox" class="custom-checkbox" id="checkAll" />
</th>
<th class="sorting_disabled"></th>
<th>@Localizer["Card name"]</th>
<th>@Localizer["RFID code"]</th>
<th class="sorting_disabled"></th>
</tr>
</thead>
<tbody class="table-body">
<tr>
<td>
<input type="checkbox" class="custom-checkbox checkbox-mark" id="checkbox_@card.RfidCardId" />
</td>
<td>
<em class="fa fa-check pointer fa-active" data-title="@SharedLocalizer[" Active "]"></em>
</td>
<td>Active</td>
<td>@card.RfidCardCode</td>
</tr>
<tr>
<td>
<input type="checkbox" class="custom-checkbox checkbox-mark" id="checkbox_@card.RfidCardId" />
</td>
<td>
<em class="fa fa-close pointer fa-inactive" data-title="@SharedLocalizer[" Inactive "]"></em>
</td>
<td> INactive</td>
<td>@card.RfidCardCode</td>
</tr>
</tbody>
</table>
推荐阅读
- r - 如何清除 R tcltk2、tcltk 中的 tk2text 框
- elasticsearch - 提供的 ElasticsearchSinkFunction 的实现是不可序列化的(flink-connector-elasticsearch6_2.11)
- azure-devops - 使用自定义构建配置在 Azure Devops 中构建 asp .net core 2.1 angular
- angular - Angular LocalStorage 安全地存储 Express API 数据
- c - printf 数组的整个单元
- julia - 将示例从 QuantEcon.jl 移植到 POMDPs.jl
- php - 如何在 PHP 中显示动态 URL?
- css - 如何使用百分比来容纳 div,不完全适合
- python - 为什么在嵌套数据结构上减少 getitem 会失败?
- javascript - Javascript:将强类型数组转换为 JSON