0

I have a list of users and each user has several roles as array. I use ag-grid to display all users along with their roles, but it looks like ag-grid does not work with my data structure. So what is the best way that I can display all user roles for each user on grid table?

data structure:

[
 {
  userId: ''
  username: '',
  userRoles: [
    {roleId: '', roleName: ''},
    ...
  ]
 },
 ...
]
4
  • In what format do you want the roles to be shown? For example, do you want to show "admin,manager,user" as a simple comma-delimited string of role names, or do you want to show something with more "structure"? Commented Aug 27, 2018 at 22:21
  • The role names can be join strings in one line, but I already figured it out myself, if you have solution, please share, it's good to learn. Commented Aug 28, 2018 at 7:33
  • 1
    I use the ColDef.valueGetter property to set the column value to a comma-delimited string containing the values: const colDef: ColDef = this.grid.columnApi.getColumn('skillLevels').getColDef(); colDef.valueGetter = this.getSkillLevels; (where this.getSkillLevels returns a string containing the comma-delimited values). Commented Aug 28, 2018 at 14:38
  • @Andy King Yep, I use similar way to achieve this. Commented Aug 29, 2018 at 7:50

1 Answer 1

1

I suggest that the best way would be to create a column group (collapsible) and put info to it.

  {
    headerName: 'Roles',
    children: [
        {
            headerName: "Top Role", field: "topRole", columnGroupShow: 'closed', width: 100, 
            cellRenderer: () => { return 'SuperAdmin'; }
        },
        {
            headerName: "Role: 1", columnGroupShow: 'open', width: 150,
            cellRenderer: () => { return 'SuperAdmin'; }
        },
        {
            headerName: "Role: 2", columnGroupShow: 'open', width: 150,
            cellRenderer: () => { return 'Support'; }
        }
    ]
  }

Here is a simple example: https://plnkr.co/edit/i3somDDT9Xdnln32xUy1?p=preview

You can define the single field to display top role from the existing and then via collapse button display all user roles.

More details about column groups

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for nice solution. In my case, I just make role names as join strings with commas then put them all in one column each row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.