javascript - DataTables : Using Row grouping, 如何获取分组条目的计数?

标签 javascript jquery datatables

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

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

enter image description here

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

请引用我的 fiddle - 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');
            }
        });
    };

谢谢

最佳答案

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

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

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

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

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

希望对您有所帮助。

关于javascript - DataTables : Using Row grouping, 如何获取分组条目的计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27908303/

相关文章:

javascript - 使用 jquery 悬停时不显示链接

jquery - 当数据库发生变化时手动渲染 meteor 模板

javascript - laravel vue 控制台中的缩进问题

javascript - 为初学者在 Javascript 中安装插件

javascript - 动态后退按钮可导航回您来自的模式

javascript - yii2 中的 Yii2 Ajax 请求

jquery - 键盘覆盖 webapp 中的文本输入(iOS)

jquery - 如何更改数据表插件中导出pdf的字体大小

javascript - 创建一个列,该列是数据表中其他两列的总和

javascript - 如何使用 ES6 语法访问导入属性