Jquery Datatables 分组插件 - 一种可扩展二级分组的方法?

发布于 2024-12-11 02:23:32 字数 269 浏览 0 评论 0原文

关于 jquery datatables 行分组插件: http://jquery-datatables- row-grouping.googlecode.com/svn/trunk/index.html ,是否可以进行两级分组并且两个分组都可以展开/折叠?我在网站上找不到任何提到这一点的内容..想知道是否有人尝试过

Concerning the jquery datatables rowgrouping plugin: http://jquery-datatables-row-grouping.googlecode.com/svn/trunk/index.html , is it possible to have the two-level grouping and also have both groupings expandable / collapsible? I couldn't find anything on the site mentioning this.. wondering if any has tried it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮光之海 2024-12-18 02:23:32

我也没有看到任何有关插件执行此操作的信息。我认为最有效的解决方案(就运行时而言)是修改 rowGrouping 插件本身,但是每次创建者更新插件时这可能会变得复杂(据我所知,扩展 jQuery 是不可能的)插件)。

不管怎样,我想出了一个解决方案。它并不漂亮,可以进行很多改进,但希望它至少能够激发一些想法。基本上,我创建了自己的 jQuery 插件,它包装了 rowGrouping 插件(您也可以单独使用中间部分 - 请参阅代码中的注释)。它实例化 rowGrouping dataTable,然后遍历行以查找每个主要组内的子组行。然后,它查找每个子组下的行,并为它们分配该组/子组组合唯一的类。最后,它使用此类作为选择器,在单击子组行时切换行。

// create our own jQuery plugin that wraps rowGrouping
(function ($) {
    $.fn.rowGroupingWithColapsableSecondLevel = function (options) {
        return this.each(function (index, elem) {
            // construct a rowGrouping object
            var oTableGrouped = $(elem).dataTable().rowGrouping(options);

            // subgroup row/tds are not uniquely identified
            // find each group row (identified by it's td having the class .group) and identify it (via a unique class per subgroup) and bind a click to the subgroup row that will toggle this class

            // SIDE NOTE: if you don't want to use the plugin wrapping method, just isolate the following "find" block and replace oTableGroup with a reference to the table object (or create an object or function with the find block)
            // example: var myTable = $('#example').dataTable().rowGrouping(); // then use myTable.find... below

            oTableGrouped.find('tbody tr td.group').each(function(index, elem) {
                var groupName = $(elem).attr('rel'); // e.g., group-1
                var tr = $(elem).parent();
                var subgroupId = 0; // incremental Id within group

                // go through subsequent rows looking for subgroups until we hit another major group (or run out of rows)
                do {
                    var tr = tr.next('tr'); // get the next row
                    if (tr.find('td').hasClass('subgroup')) {
                        // this is a subgroup row
                        subgroupId ++;
                        // give this subgroup an id so we can work with it
                        tr.attr('id', groupName + '-subgroup-' + subgroupId);
                        // assign parent group id as class so it will be toggled with other rows
                        tr.addClass('group-item-' + groupName);
                        // assign a toggle function
                        tr.click( function() {
                            $('.' + $(this).attr('id')).toggle();
                        });
                    } else if(tr.find('td').hasClass('group')) {
                        // we've reached the next group, exit the do loop (the next group will be picked up by the next oTableGroup.find)
                        break;
                    } else if(tr.length == 1) {
                        // this is a row under the subgroup, identify it by adding a class
                        tr.addClass(groupName + '-subgroup-' + subgroupId);
                    }
                } while (tr.length == 1);
            }); // end of the find block

            return oTableGrouped;
        })
    };
})(jQuery);

以下是您将如何使用它:

$(function() {
    var oTable = $('#example').dataTable({ "bLengthChange": false, "bPaginate": false}).rowGroupingWithColapsableSecondLevel({  "iGroupingColumnIndex2": 1 , "bExpandableGrouping": true });
});

希望这会有所帮助。干杯。

I didn't see anything either about the plugin doing this. I think the most efficient solution (in terms of runtime) would be to modify the rowGrouping plugin itself, but that might get complicated every time the creator updates the plugin (and to the best of my knowledge, it's not really possible to extend a jQuery plugin).

Anyway, I came up with a solution. It's not pretty and could stand to use a lot of improvement, but hopefully it serves to spark some ideas at the very least. Basically I created my own jQuery plugin that wraps the rowGrouping plugin (you could also just use the middle part by itself - see notes in code). It instantiates a rowGrouping dataTable then traverses the rows looking for subgroup rows within each major group. Then it finds rows under each subgroup and assigns them a class unique to that group/subgroup combination. Finally, it uses this class as a selector to toggle the rows when a subgroup row is clicked.

// create our own jQuery plugin that wraps rowGrouping
(function ($) {
    $.fn.rowGroupingWithColapsableSecondLevel = function (options) {
        return this.each(function (index, elem) {
            // construct a rowGrouping object
            var oTableGrouped = $(elem).dataTable().rowGrouping(options);

            // subgroup row/tds are not uniquely identified
            // find each group row (identified by it's td having the class .group) and identify it (via a unique class per subgroup) and bind a click to the subgroup row that will toggle this class

            // SIDE NOTE: if you don't want to use the plugin wrapping method, just isolate the following "find" block and replace oTableGroup with a reference to the table object (or create an object or function with the find block)
            // example: var myTable = $('#example').dataTable().rowGrouping(); // then use myTable.find... below

            oTableGrouped.find('tbody tr td.group').each(function(index, elem) {
                var groupName = $(elem).attr('rel'); // e.g., group-1
                var tr = $(elem).parent();
                var subgroupId = 0; // incremental Id within group

                // go through subsequent rows looking for subgroups until we hit another major group (or run out of rows)
                do {
                    var tr = tr.next('tr'); // get the next row
                    if (tr.find('td').hasClass('subgroup')) {
                        // this is a subgroup row
                        subgroupId ++;
                        // give this subgroup an id so we can work with it
                        tr.attr('id', groupName + '-subgroup-' + subgroupId);
                        // assign parent group id as class so it will be toggled with other rows
                        tr.addClass('group-item-' + groupName);
                        // assign a toggle function
                        tr.click( function() {
                            $('.' + $(this).attr('id')).toggle();
                        });
                    } else if(tr.find('td').hasClass('group')) {
                        // we've reached the next group, exit the do loop (the next group will be picked up by the next oTableGroup.find)
                        break;
                    } else if(tr.length == 1) {
                        // this is a row under the subgroup, identify it by adding a class
                        tr.addClass(groupName + '-subgroup-' + subgroupId);
                    }
                } while (tr.length == 1);
            }); // end of the find block

            return oTableGrouped;
        })
    };
})(jQuery);

And here's how you would use it:

$(function() {
    var oTable = $('#example').dataTable({ "bLengthChange": false, "bPaginate": false}).rowGroupingWithColapsableSecondLevel({  "iGroupingColumnIndex2": 1 , "bExpandableGrouping": true });
});

Hope this helps. Cheers.

西瓜 2024-12-18 02:23:32

subgroupId 的初始化应该在每次调用之前

var subgroupId = 0; // incremental Id within group
oTableGrouped.find('tbody tr td.group').each(function(index, elem) {

..
..
..
}

the initialization of subgroupId should be before the each call

var subgroupId = 0; // incremental Id within group
oTableGrouped.find('tbody tr td.group').each(function(index, elem) {

..
..
..
}
冷夜 2024-12-18 02:23:32

将这两个布尔值设置为 true:

bExpandableGrouping: true
bExpandableGrouping2: true

Set both of these booleans to true:

bExpandableGrouping: true
bExpandableGrouping2: true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文