【问题标题】:Datatables highlight rows beyond the first paginated page when table cells match当表格单元格匹配时,数据表突出显示第一个分页页面之外的行
【发布时间】:2015-07-13 04:43:41
【问题描述】:

当我试图突出显示第一个分页之外的行时,Datatables 插件给了我一些问题。

正如您将在下面的示例 JSFiddle 中看到的,我在加载时对位置列进行排序。当一行中有 2 个或更多相关位置时,我们会突出显示父表行。我遇到的问题是,如果第一页上有一个位置在最后一个表行中并且与第一个位置相匹配第二页 第一页的最后一个表格行未突出显示。我在这里使用的示例是开发人员职位。

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", "");
            }
        });
    }

});

【问题讨论】:

    标签: javascript jquery datatables


    【解决方案1】:

    在 DataTables 中,DOM 中仅存在可见行,这就是为什么 next()prev() 不能按预期工作的原因。

    您可以使用rows({ search: 'applied' ).nodes() 来获取应用过滤的所有行。

    此外,由于您正在处理整个数据集,因此附加到 order.dt 事件而不是在排序时仅突出显示一次行是有意义的。

    有关代码和演示,请参阅updated jsFiddle

    不过我建议看Row grouping example,它在可用性和代码方面似乎更好。

    【讨论】:

      【解决方案2】:

      highlight 函数中,您只查看当前页面的数据。这就是为什么“Developer”行没有看到它实际上在其他页面上有重复的行。

      你应该做的是查看整个表的数据。您可以通过在 highlight 函数的开头调用以下代码来做到这一点:

      var data = $('table').dataTable().fnGetData();
      

      在当前页面的每一行中,您可以通过调用来查看表中是否存在重复行某处

      var isDup = $.grep(data, function(d) { return d[1] === $current.text(); }).length > 1;
      

      根据这个isDup var,您可以为信息和边框底部放置 css

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-29
        • 1970-01-01
        • 1970-01-01
        • 2018-01-04
        • 1970-01-01
        • 1970-01-01
        • 2012-08-06
        • 1970-01-01
        相关资源
        最近更新 更多