2

I want to use Angular Material table. I've got a function, which gives me back the sales array, what I want to display. This function works fine, but I can't see anything on my table. If I diplay the sales with a simple table, it works, so I got some problems with the datasource variable...

export class SaleListingComponent implements OnInit {
constructor(private saleService: SaleDomainService, private router:Router) { }

  public sales: Sale[];
  dataSource=this.sales;

  ngOnInit() {
    this.loadSales();

  }

  private loadSales() {
    this.saleService.getSales().subscribe(
      data => { this.sales = data },
      err => console.error(err),
      () => console.log("sales loaded.", JSON.stringify(this.sales))      
    );

  } 
}
 <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

    <ng-container matColumnDef="id">
      <th mat-header-cell *matHeaderCellDef> No. </th>
      <td mat-cell *matCellDef="let element"> {{element.id}} </td>
    </ng-container>

    <ng-container matColumnDef="amount">
      <th mat-header-cell *matHeaderCellDef> amount </th>
      <td mat-cell *matCellDef="let element"> {{element.amount}} </td>
    </ng-container>

    <ng-container matColumnDef="date">
      <th mat-header-cell *matHeaderCellDef> date </th>
      <td mat-cell *matCellDef="let element"> {{element.date}} </td>
    </ng-container>

    <ng-container matColumnDef="industry">
      <th mat-header-cell *matHeaderCellDef> industry </th>
      <td mat-cell *matCellDef="let element"> {{element.industry}} </td>
    </ng-container>

        <ng-container matColumnDef="price">
          <th mat-header-cell *matHeaderCellDef> price </th>
          <td mat-cell *matCellDef="let element"> {{element.price}} </td>
        </ng-container>

    <ng-container matColumnDef="prod">
      <th mat-header-cell *matHeaderCellDef> prod </th>
      <td mat-cell *matCellDef="let element"> {{element.prod}} </td>
    </ng-container>

        <ng-container matColumnDef="productType">
          <th mat-header-cell *matHeaderCellDef> productType </th>
          <td mat-cell *matCellDef="let element"> {{element.productType}} </td>
        </ng-container>

        <ng-container matColumnDef="salesman">
          <th mat-header-cell *matHeaderCellDef> salesman </th>
          <td mat-cell *matCellDef="let element"> {{element.salesman}} </td>
        </ng-container>      

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  </table>
2
  • change [dataSource]="dataSource" to `[dataSource]="sales" Commented Nov 23, 2019 at 15:41
  • I added an answer can you please mark it as correct answer ? Commented Nov 23, 2019 at 15:46

1 Answer 1

1

change [dataSource]="dataSource" to `[dataSource]="sales"

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.