当 table 个单元格匹配时,Datatables 突出显示第一个分页页面以外的行
Datatables highlight rows beyond the first paginated page when table cells match
Datatables 插件在尝试突出显示第一个分页页面以外的行时出现一些问题。
正如您将在下面的示例 JSFiddle 中看到的,我在加载时对位置列进行排序。当连续有 2 个或更多相关位置时,我们突出显示父 table rows.The 我遇到的问题是,如果第一页上有一个位置在最后 table行并且与第二页的第一个位置匹配第一页的最后 table 行未突出显示。我在这里使用的示例是开发人员职位。
JSFiddle
https://jsfiddle.net/ebRXw/563/
JS
$(document).ready(function () {
$('table').dataTable({
"paging": true,
"ordering": true,
"filter": false,
"length": false,
"info": false,
"order": [
[1, "asc"]
]
});
highlight();
$('table').on('draw.dt', function () {
if ($("thead th:nth-child(2)").hasClass("sorting_desc") || $("thead th:nth-child(2)").hasClass("sorting_asc")) {
highlight();
} else {
$("td").removeClass("info");
$("td").css("border-bottom", "");
}
});
function highlight() {
var duplicate = false;
$("table tbody tr").each(function () {
var $current = $(this).children(":nth-child(2)");
var $next = $(this).next().children(":nth-child(2)");
if ($current.text() === $next.text() && !duplicate) {
duplicate = true;
$current.parent().children().addClass("info");
$current.parent().prev().children().css("border-bottom", "1px solid #333");
} else if ($current.text() === $next.text() && duplicate) {
$current.parent().children().addClass("info");
} else if ($current.text() !== $next.text() && duplicate) {
$current.parent().children().addClass("info");
$current.parent().children().css("border-bottom", "1px solid #333");
duplicate = false;
} else {
$current.parent().children().removeClass("info");
$current.parent().children().css("border-bottom", "");
}
});
}
});
在highlight
函数中,您只查看当前页面的数据。这就是为什么“Developer”行看不到它实际上在其他页面上有重复行的原因。
你应该看看整个table的数据。您可以通过在 highlight
函数的开头调用以下代码来执行此操作:
var data = $('table').dataTable().fnGetData();
在当前页面的每一行中,您可以通过调用以下命令查看 某处 中是否有重复行 table:
var isDup = $.grep(data, function(d) { return d[1] === $current.text(); }).length > 1;
根据此 isDup
变量,您可以放置 css 用于信息和边框底部
在 DataTables 中,DOM 中只存在可见行,这就是为什么 next()
和 prev()
没有按预期工作的原因。
您可以使用 rows({ search: 'applied' ).nodes() 获取所有应用过滤的行。
此外,由于您正在处理整个数据集,因此附加到 order.dt
事件而不是在排序时仅突出显示行一次是有意义的。
有关代码和演示,请参阅 updated jsFiddle。
但是我更推荐看Row grouping example,它似乎在可用性和代码方面更好。
Datatables 插件在尝试突出显示第一个分页页面以外的行时出现一些问题。
正如您将在下面的示例 JSFiddle 中看到的,我在加载时对位置列进行排序。当连续有 2 个或更多相关位置时,我们突出显示父 table rows.The 我遇到的问题是,如果第一页上有一个位置在最后 table行并且与第二页的第一个位置匹配第一页的最后 table 行未突出显示。我在这里使用的示例是开发人员职位。
JSFiddle
https://jsfiddle.net/ebRXw/563/
JS
$(document).ready(function () {
$('table').dataTable({
"paging": true,
"ordering": true,
"filter": false,
"length": false,
"info": false,
"order": [
[1, "asc"]
]
});
highlight();
$('table').on('draw.dt', function () {
if ($("thead th:nth-child(2)").hasClass("sorting_desc") || $("thead th:nth-child(2)").hasClass("sorting_asc")) {
highlight();
} else {
$("td").removeClass("info");
$("td").css("border-bottom", "");
}
});
function highlight() {
var duplicate = false;
$("table tbody tr").each(function () {
var $current = $(this).children(":nth-child(2)");
var $next = $(this).next().children(":nth-child(2)");
if ($current.text() === $next.text() && !duplicate) {
duplicate = true;
$current.parent().children().addClass("info");
$current.parent().prev().children().css("border-bottom", "1px solid #333");
} else if ($current.text() === $next.text() && duplicate) {
$current.parent().children().addClass("info");
} else if ($current.text() !== $next.text() && duplicate) {
$current.parent().children().addClass("info");
$current.parent().children().css("border-bottom", "1px solid #333");
duplicate = false;
} else {
$current.parent().children().removeClass("info");
$current.parent().children().css("border-bottom", "");
}
});
}
});
在highlight
函数中,您只查看当前页面的数据。这就是为什么“Developer”行看不到它实际上在其他页面上有重复行的原因。
你应该看看整个table的数据。您可以通过在 highlight
函数的开头调用以下代码来执行此操作:
var data = $('table').dataTable().fnGetData();
在当前页面的每一行中,您可以通过调用以下命令查看 某处 中是否有重复行 table:
var isDup = $.grep(data, function(d) { return d[1] === $current.text(); }).length > 1;
根据此 isDup
变量,您可以放置 css 用于信息和边框底部
在 DataTables 中,DOM 中只存在可见行,这就是为什么 next()
和 prev()
没有按预期工作的原因。
您可以使用 rows({ search: 'applied' ).nodes() 获取所有应用过滤的行。
此外,由于您正在处理整个数据集,因此附加到 order.dt
事件而不是在排序时仅突出显示行一次是有意义的。
有关代码和演示,请参阅 updated jsFiddle。
但是我更推荐看Row grouping example,它似乎在可用性和代码方面更好。