繁体   English   中英

Ag-Grid - 如何按 2 列分组,但在同一行上有值

[英]Ag-Grid - How to group by 2 columns, but have the values on the same row

不知道如何准确地用文字解释这一点,但我认为举几个例子就会有道理

第一个示例中,我仅按一列location分组。 在位置字段中,我在路径中使用空格,即location: ['Home', ''], (1 个空格), location: ['Home', ' '], (2 个空格) 所以我可以有 2其他列作为主行的“详细信息”(即在此示例中清洁器类型和清洁器重量)

看起来像这样..

在此处输入图片说明

也许有更好的方法来做到这一点,但如果我只想按一列分组,这将在上面工作。

但是,我实际上想按列分组,但只有那些“嵌套”的 2 个详细信息(子)记录)

在上面,我也想按房间分组,但没有房间作为嵌套行。

所以,在上面我有(location=Home, room=room1) ,但我也可能有(location=Home, room=room2) ,即我有room2

例如想要这个...

在此处输入图片说明

我已经尝试了各种各样的事情,但就是不能把这种观点(我总是不需要嵌套结束),例如,这是我不想在这里输入链接的描述

有没有办法做到这种观点?

提前致谢!

我从这篇文章中找到了做到这一点的方法

所以在这个例子中,你可以使用一个完全不同的属性在网格中根本没有显示出来,让getDataPath来控制分组,然后使用valueGetter将你想要的放在分组列中。

所以我可以有以下......

    var gridOptions = {
        columnDefs: [
            // we're using the auto group column by default!
            { field: 'room',resizable: true, },
            { field: 'cleanertype', resizable: true, },    
            { field: 'cleanerweight',aggFunc: 'sum', resizable: true, },
            { field: 'totalweight', resizable: true, },
        ],
        defaultColDef: {
            flex: 1,
        },

        autoGroupColumnDef: {
            headerName: 'location',
            sortable: true,
            resizable: true,
            filter: 'agTextColumnFilter',
            valueGetter: (params) => {
                return params.node.data.location; // <--- what to show in the group column
            },
            cellRendererParams: {
                suppressCount: true,
            },
        },
        rowData: rowData,
        treeData: true, // enable Tree Data mode
        animateRows: true,
        groupDefaultExpanded: -1, // expand all groups by default
        getDataPath: function (data) {
            return data.grouping;  // <= how to group/nest
        },
    };

和数据...

        var rowData = [
        // Home/room1
        {
            grouping: ['1'],
            location: 'Home',
            room: 'room1',
            cleanertype: '',    
            totalweight: 60
        },
        {
            grouping: ['1', '1'],    
            cleanertype: 'type1',
            cleanerweight:10,
        },
        {
            grouping: ['1', '2'],    
            cleanertype: 'type2',
            cleanerweight:10,
        },
        // Home/room2
        {
            grouping: ['2'],
            location: 'Home',
            room: 'room2',
            cleanertype: '',    
            totalweight: 60
        },
        {
            grouping: ['2', '1'],   
            cleanertype: 'type1',
            cleanerweight:10,
        },
        {
            grouping: ['2', '2'],    
            cleanertype: 'type2',
            cleanerweight:10,
        },

        {
            grouping: ['3'],
            location: 'Work',
            room: 'room1',
            cleanertype: '',
            cleanerweight: 30,
            totalweight: 60
        },
        {
            grouping: ['3', '1'],               
            cleanertype: 'general',
            cleanerweight:10,
        },

        {
            grouping: ['3', '2'],               
            cleanertype: 'heavyduty',
            cleanerweight: 10,
        },          
    ];

所以我使用grouping来控制分组/嵌套,并且工作完美,这正是我所追求的。

可以在此处查看工作示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM