在datatable中组合一个字母和名称的列 [英] Group a single column with letter and name in datatable?

查看:95
本文介绍了在datatable中组合一个字母和名称的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 rowGrouping 插件对单列进行字母和名称分组-in和jQuery DataTables?

How can we have letter and name grouping for a single column using rowGrouping plug-in and jQuery DataTables?

例如:对于值:

aaa=>1,aaa=>2,aaa=>3,aab=>1,aab=>2,aab=>3,aac=>1,aac=>2,...

A B ,然后使用名称 aaa aab aac

should group (sGroupBy) with letter A, B, then with name aaa, aab, aac.

推荐答案


解决方案

插件 rowGrouping 不再开发,我不建议使用它。使用替代方法执行行分组,如行分组示例所示。

Plug-in rowGrouping is no longer being developed, I would not recommend using it. Use alternative way to perform row grouping as shown in Row grouping example.

例如,使用下面的代码按列包含名称排序时按第一个字母对行进行分组。

For example, use the code below to group rows by first letter when sorted by column containing names.

var table = $('#example').DataTable({
    "drawCallback": function (settings){
        var api = this.api();

        // Zero-based index of the column containing names
        var col_name = 0;

        // If ordered by column containing names
        if (api.order()[0][0] === col_name) {
            var rows = api.rows({ page: 'current' }).nodes();
            var group_last = null;

            api.column(col_name, { page: 'current' }).data().each(function (name, index){
                var group = name.substr(0, 1);

                if (group_last !== group) {
                    $(rows).eq(index).before(
                        '<tr class="group"><td colspan="6">' + group + '</td></tr>'
                    );

                    group_last = group;
                }
            });
        }
    }
});




DEMO

请参阅此jsFiddle 进行代码和演示。

See this jsFiddle for code and demonstration.

这篇关于在datatable中组合一个字母和名称的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆