1

I try to print in Mat-table my Data.

I'm very close but I'm not able to complete my front end.

My data are :

0: {Group: 1, Rules: Array(1), Title: "NAME"}
1: {Group: 2, Rules: Array(2), Title: "TERRITORY"}

And the array for Group 1 have rules like this (and so on for Group 2):
0: {Date: "1990-01-01", NewRules: 0, Rules: "Ligue Name", Id: 1}
1: {Date: "1990-01-01", NewRules: 1, Rules: "Other rules", Id: 2}

here's the .html code:

<mat-card-content>
  <table mat-table [dataSource]="rules$ | async" class="mat-elevation-z8">

    <!-- DateModifier Column -->
    <ng-container matColumnDef="Date">
      <th mat-header-cell *matHeaderCellDef>Date</th>
      <td mat-cell *matCellDef="let element">
        {{ element.Reules[0].Date }}
      </td>
    </ng-container>

    <!-- Reglement Column -->
    <ng-container matColumnDef="Rules">
      <th mat-header-cell *matHeaderCellDef>Rule</th>
      <td mat-cell *matCellDef="let element">
        {{ element.Rules[0].Rules }}
      </td>
    </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
 </table>
</mat-card-content>

What I've got it's ok If I specify : element.Rules[0].Rules

But I want to loop through each item of the array.

Giving something

Date       Rules

1990/1/1  Ligue Name

1990/1/1  Other Rules

Also if you can give me this hint too. How Can I print for each Group the title before all the rules ?

Group : NAME

Date       Rules

1990/1/1  Ligue Name

1990/1/1  Other Rules

Group: TERRITORY
 etc....

file .ts:

import { Component, OnInit, Input } from "@angular/core";
import { Reglements } from "../models/models";
import { MatDialog } from "@angular/material";
import { FirebaseService } from "../service/firebase.service";
import { Observable } from "rxjs";

@Component({
  selector: "app-reglements",
  templateUrl: "./reglements.component.html",
  styleUrls: ["./reglements.component.css"]
})
export class ReglementsComponent implements OnInit {
  displayedColumns: string[] = ["Date", "Rules"];

  @Input()
  // public boardDirection: AngularFireList<Direction[]>;
  public reglements$: Observable<Reglements[]>;

  constructor(
    private dialog: MatDialog,
    private firebaseService: FirebaseService
  ) {
    this.reglements$ = firebaseService.loadAllRules();
    this.reglements$.forEach(element => {
      console.log("eleem", element);
    });
  }

  ngOnInit() {}
}

Thanks

5
  • can you post the datasource object also. it will help to understand more clearly Commented Jan 31, 2020 at 20:25
  • I add the .ts file in the code. The datasource is in a firebase. I give you the way it is build: Group: number , Rules: string[], title: string. And the Rules has : Date: string , NewRules: number, Rules: string, Id: number Commented Jan 31, 2020 at 20:48
  • I was asking about exact data that you are using for material table. i want to see the value for rules$ variable Commented Jan 31, 2020 at 20:50
  • like I said : the Rules has : Date: string , NewRules: number, Rules: string, Id: number .... Or in data: {Date: "1990-01-01", NewRules: 0, Rules: "Ligue Name", Id: 1} .Did I understand correctly your request :) Commented Jan 31, 2020 at 20:54
  • ok got it. working on it Commented Jan 31, 2020 at 20:56

2 Answers 2

6

What I understood in your question is that you have a dictionary of group, rules and title. Among them, rules is a array of dictionaries and you want to loop through that dictionary and print it in table. So, this is how your datasource looks like:

dataSource = [
               {group: 1, rules: [{'date': '2019-1-31', 'rules': 'Some Rule', id: 1}, {'date': '2019-1-27', 'rules': 'Some Rule', id: 2}], title: 'name'},
               {group: 2, rules: [{'date': '2019-1-31', 'rules': 'Some Rule', id: 1}, {'date': '2019-1-27', 'rules': 'Some Rule', id: 2}], title: 'TERRITORY'}
             ]

After modifying your code:

 <table mat-table [dataSource]="dataSource">
     <ng-container matColumnDef="yourColName">
       <th mat-header-cell *matHeaderCellDef>Group</th>
       <td mat-cell *matCellDef="let element">
           {{element.group}}
       </td>
     </ng-container>
     <ng-container matColumnDef="yourColName">
       <th mat-header-cell *matHeaderCellDef>Rules Array Object</th>
       <td mat-cell *matCellDef="let element">
          <!--THIS IS WHERE YOU ARE ITERATING THROUGH ARRAY OF RULES OBJECT-->
           <ng-container *ngFor="let rule of element.rules">
               {{ rule.date}}, {{rule.rules}}, {{rule.id}}
           </ng-container>
       </td>
     </ng-container>
 </table>
Sign up to request clarification or add additional context in comments.

1 Comment

You get all the upvotes my friend <3 This answer helped me big time. God Bless
3

If you want to loop through each Rule in given data you can use nested table. I have created some code for nested table using angular material click here

5 Comments

Great, that's close of what I want. Is it possible to extract the tile to have 1 line with the title only, and under the title add the table rules ? And so on ? very close of what I'm looking for
Well maybe your Part 2 seems to be what I'm looking for. Let me dig and I'll comeback. thanks
Ok Part 2 is very close of what I'm looking for. The last problem is that GROUP appear on the same line of the table instead to be like a title. How I can add a line feed after the group section to put the table under ? (I try to add a <br> ) and it doesn't work.
Your code is working. So I create a StackBlitz with my code to try it... and it works too (The Title is above the rules table). The problem when I launch it with NPM Start in my localhost, the title appear as a column left beside the rules table. And the code that I try on Stackblitz is the same. The difference is I get my data from Firebase. So I change it to use the DataSource in the code... Same problem. Title appear as a column on my localhost, but is OK in the StackBlitz... very curious. But thanks you respond for what I was looking for. Now I need to find Why I have this problem.
What about looping through column names that can be retrieved by capitalizing the first letters of each column field?

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.