Group data with NgFor and GroupBy

2

Before in Angular 1 when I wanted to do a grouping of data it was like this:

ng-repeat="(key, value) in me | groupBy: 'role_id'"

In my object me had a list of users with a different role_id field. Hence I wanted to do this:

# Admin
Diego
João
Maria
# User
Márcio
Alex
Daniella

Only with this command line could I cluster. But I can not do that in Angular 2 (TypeScript) using Ionic.

What command is similar to this one to use in Angular 2?

I'm using NgFor , but I can not do pooling.

    
asked by anonymous 14.06.2017 / 21:29

1 answer

2

The filters of AngularJS has not changed to Angular2 and there are no (many) pipes that exist by default.

So first you have to do @Pipe() " group_by " and then yes, you can use it.

However, you can always group before doing ngFor for example when you receive the data;

Something like:

const role = "dev"
const groupedArray = me.filter(entry => entry.role_id === role);

and then iterate under groupedArray instead of me :

<div *ngFor="let user in groupedArray">
    {{user.name}} {{user.role_id}} {{user.cenas}}
</div>
    
21.06.2017 / 16:05