0

Can someone help me with angular material table? Getting data from firebase, it shows if I use snapshotChanges() but data doesn't appear when I return array with custom data.

let prdarr= [];
this.db.list('/products').snapshotChanges()
 .subscribe(products => {
     products.forEach(prddata => {
     let prd: any = prddata.payload.val();
     prdarr.push({
        PID: prddata.key,
        Name: prd.name,
     });
   });
});
return prdarr;

This works fine

return this.db.list('/products').snapshotChanges();

Markup

    <table mat-table [dataSource]="dataSource" multiTemplateDataRows class="mat-elevation-z8">
    <ng-container matColumnDef="{{column}}" *ngFor="let column of columnsToDisplay">
        <th mat-header-cell *matHeaderCellDef> {{column}} </th>
        <td mat-cell *matCellDef="let element"> {{element[column]}} </td>
    </ng-container>

    <!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
    <ng-container matColumnDef="expandedDetail">
        <td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplay.length">
            <div class="example-element-detail" [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
                <div class="example-element-diagram">
                    <div class="example-element-position"> {{element.position}} </div>
                    <div class="example-element-symbol"> {{element.symbol}} </div>
                    <div class="example-element-name"> {{element.name}} </div>
                    <div class="example-element-weight"> {{element.weight}} </div>
                </div>
                <div class="example-element-description">
                    {{element.description}}
                    <span class="example-element-description-attribution"> -- Wikipedia </span>
                </div>
            </div>
        </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
    <tr mat-row *matRowDef="let element; columns: columnsToDisplay;"
    class="example-element-row"
    [class.example-expanded-row]="expandedElement === element"
    (click)="expandedElement = expandedElement === element ? null : element"></tr>
    <tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>
4
  • Firebase is asynchronous. So basicly when you return the array firebase hasn't finished getting the data. I currently don't have the energy to help you more but if you search for this you will find plenty of information about how to handle this. Commented Jul 4, 2019 at 10:18
  • Share the template file.
    – leopal
    Commented Jul 4, 2019 at 10:52
  • @AndréKool I thought of that but I console logged in the array in the component, I am getting the data. only difference being [] instead of (2)[{...},{...}] but I am getting the required data if I check the array. Don't know if I'm missing something or I don't understand the depth of it. It would be great if you could help me fix it. Commented Jul 4, 2019 at 19:55
  • @leopal I have added the markup Commented Jul 4, 2019 at 20:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.