0

Hi i have a component where in the html have a code like

<div *ngFor="let group of groups; index as index">
  <div *ngIf="group0.lenght > 0">
     <mat-table [datasource]="'group'+index">

     </mat-table>
  </div>
</div>

before i have a code that filter the items by the groupid like a foreign key

group0 = this.items.filter(x => x.group_uuid == this.groups[i].uuid)
groupn = this.items.filter(x => x.group_uuid == this.groups[n].uuid)

but the problem is when the table appears, i got the next error: ERROR Error: Provided data source did not match an array, Observable, or DataSource

any idea? thanks in advance

4
  • what is the type for this.items? an array of what? Commented Aug 4, 2021 at 22:01
  • 1
    You need to put all your groups in an array. Right now you pass a string to datasource instead of an array. It's the same as trying to call a variable by string with name of the variable. As long the c = a + b not equal to c = 'a' + 'b' code above will not be working. Commented Aug 4, 2021 at 22:03
  • What is group0 in line 2? Commented Aug 4, 2021 at 22:11
  • what is your definition of "groups"? Commented Aug 4, 2021 at 22:11

1 Answer 1

1

You need store groups in an array.

groupsOfItems = this.groups
  .map(group => this.items.filter(x => x.group_uuid === group.uuid));

in template:

<div *ngFor="let group of groupsOfItems; index as index">
  <div *ngIf="group.lenght > 0">
     <mat-table [datasource]="group">

     </mat-table>
  </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

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.