【问题标题】:DataTables : Using Row grouping, how do I get a count of grouped entries?DataTables:使用行分组,我如何获得分组条目的计数?
【发布时间】:2015-01-13 03:05:50
【问题描述】:

我正在使用 Datatables 创建一个按数据分组的网格。我能够对我的表进行分组。

我想将条目总数作为分组行数而不是作为总条目数,我该怎么做?

上图中突出显示的部分应显示“显示 8 个条目中的 1 到 8 个”,因为有 8 个组。

请参考我的小提琴 - http://jsfiddle.net/hh4kq5y0/

 $(document).ready(function () {
        $('#example').dataTable({
            "bLengthChange": false,
            "bPaginate": false,
            "bJQueryUI": true
        }).rowGrouping({
            bExpandableGrouping: true,
            bExpandSingleGroup: false,
            iExpandGroupOffset: -1,
            asExpandedGroups: [""]
        });

        GridRowCount();
    });

    function GridRowCount() {
        $('span.rowCount-grid').remove();
        $('input.expandedOrCollapsedGroup').remove();

        $('.dataTables_wrapper').find('[id|=group-id]').each(function () {
            var rowCount = $(this).nextUntil('[id|=group-id]').length;
            $(this).find('td').append($('<span />', { 'class': 'rowCount-grid' }).append($('<b />', { 'text': rowCount })));
        });

        $('.dataTables_wrapper').find('.dataTables_filter').append($('<input />', { 'type': 'button', 'class': 'expandedOrCollapsedGroup collapsed', 'value': 'Expanded All Group' }));

        $('.expandedOrCollapsedGroup').live('click', function () {
            if ($(this).hasClass('collapsed')) {
                $(this).addClass('expanded').removeClass('collapsed').val('Collapse All Group').parents('.dataTables_wrapper').find('.collapsed-group').trigger('click');
            }
            else {
                $(this).addClass('collapsed').removeClass('expanded').val('Expanded All Group').parents('.dataTables_wrapper').find('.expanded-group').trigger('click');
            }
        });
    };

谢谢

【问题讨论】:

    标签: javascript jquery datatables jquery-datatables


    【解决方案1】:

    DataTables 将此 Show x to y from z 文本默认设置为

    "sInfo": "Showing _START_ to _END_ of _TOTAL_ entries"
    

    查看源代码,三个占位符只是被内部变量替换。您也许可以更改它们,但我不确定,也不建议这样做。

    如果您想更改该信息显示,您可以在GridRowCount 函数中使用一些 jQuery,您可以通过以下方式访问分组行的总数

    $('.dataTables_wrapper').find('[id|=group-id]').length
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-06
      • 2020-09-11
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 2023-02-07
      相关资源
      最近更新 更多