Group data with Ngfor and Groupby

Asked

Viewed 639 times

2

Before in Angular 1 when I wanted to make a grouping of data I did so:

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

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

# Admin
Diego
João
Maria

# User
Márcio
Alex
Daniella

Only with this command line could I group. But I’m not able to do that in Angular 2 (Typescript), using Ionic.

Which command is similar to this to use at Angular 2?

I’m using the NgFor, but I can’t do the grouping.

  • 1

    Related subject: https://stackoverflow.com/questions/37248580/how-to-group-data-in-angular-2 and https://stackoverflow.com/questions/35890413/angular2-typescript-nested-ngfor-without-nested-object

  • Until it helped. I understood a little about it. But it is giving error in a Foreach.

1 answer

2


The filters of Angularjs did not pass to Angular2 and do not exist (many) pipes that exist by default.

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

Yet you can always group together before doing the ngFor for example when receiving data;

Something like:

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

and then iterate under the groupedArray instead of me:

<div *ngFor="let user in groupedArray">
    {{user.name}} {{user.role_id}} {{user.cenas}}
</div>
  • What is the definition of @Pipe?

  • @Diegosouza @Pipe() is the equivalent of filter; This is a class, normal, decorated with @Pipe() for Angular to know "what is what" - in the same way that there are the decorators of @Component()

  • What is @Decorator?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.