rxdb shema { "id": "test", "color": "test", "hp": 100, "snrcn": "test", "contact": [ {…
- User
rxdb shema { "_id": "test", "color": "test", "hp": 100, "snrcn": "test", "cont_act": [ { "emil": "0" }, { "emil": "1" }, { "emil": "2" }, { "emil": "3" }, { "emil": "4" } ], "_rev": "1-3a5afadc2e2b7e2f7afcb8523a388f08" }
ngfor cont_act tr table in angular
- Assistant
To use ngFor in Angular to display the cont_act array as a table, you can use the following code:
In your component.ts file:
import { Component } from '@angular/core'; import { RxCollection } from 'rxdb'; @Component({ selector: 'app-table', templateUrl: './table.component.html', }) export class TableComponent { collection: RxCollection; constructor() { this.collection = // your collection from RxDB } }In your component.html file:
<table> <thead> <tr> <th>Index</th> <th>Email</th> </tr> </thead> <tbody> <tr *ngFor="let contact of collection.cont_act; let i = index"> <td>{{ i + 1 }}</td> <td>{{ contact.emil }}</td> </tr> </tbody> </table>This code will iterate through the cont_act array in your RxDB collection and display each email as a row in a table. The
*ngFordirective is used to loop through the contacts in the array.let i = indexis used to get the index of the current contact, which is used to get the row number. - User
<ng-container *ngFor="let herod of heroes$.cont_act; | async as heroes; index as i; let last = last"> {{herod.emil}}
- Assistant
The code you provided is using the
ngFordirective along with the async pipe to iterate through thecont_actarray and display theemilproperty of each object inside it.The
*ngFordirective is used to create a template for each element of theheroes$observable. Theasyncpipe is used to subscribe to and unwrap the observable value.The
askeyword is used to assign a new local variableheroesto the unwrapped value of the observable. Theindex as isyntax is used to get the current index of the loop iteration.Finally,
let last = lastis used to create a local variablelastthat will betruefor the last iteration of the loop.Inside the
ng-containertag, a paragraph element is used to display the value ofherod.emil, which is the email property of each object inside thecont_actarray.Overall, this code will display the email property of each object in the
cont_actarray that is obtained from theheroes$observable. - User
"_id": "test", "_rev": "1-3a5afadc2e2b7e2f7afcb8523a388f08", "value": { "rev": "1-3a5afadc2e2b7e2f7afcb8523a388f08" }, "key": "test", "doc": { "color": "test", "hp": 100, "snrcn": "test", "cont_act": [ { "emil": "0" }, { "emil": "1" }, { "emil": "2" }, { "emil": "3" }, { "emil": "4" } ], "_id": "test",
<ng-container *ngFor="let herod of heroes$.cont_act; | async as heroes; index as i; let last = last"> {{herod.emil}}
- Assistant
The JSON object you provided has a different structure than the one used in the previous example. In this case, instead of having the
cont_actarray as a direct property of the object, it is nested inside thedocsub-object.To display the emails in the
cont_actarray usingngForand the async pipe, you need to access thedocproperty first and then iterate through thecont_actarray, like this:<ng-container *ngIf="heroes$ | async as data"> <ng-container *ngFor="let hero of data.rows"> <ng-container *ngIf="hero.doc?.cont_act"> <ng-container *ngFor="let contact of hero.doc.cont_act; let i = index; let last = last"> <p>{{ contact.emil }}</p> </ng-container> </ng-container> </ng-container> </ng-container>Here,
heroes$is an observable which returns an array of objects. Each object has a property which contains the array. - User
“_id”: “test”, “_rev”: “1-3a5afadc2e2b7e2f7afcb8523a388f08”, “value”: { “rev”: “1-3a5afadc2e2b7e2f7afcb8523a388f08” }, “key”: “test”, “doc”: { “color”: “test”, “hp”: 100, “snrcn”: “test”, “cont_act”: [ { “emil”: “0” }, { “emil”: “1” }, { “emil”: “2” }, { “emil”: “3” }, { “emil”: “4” } ], “_id”: “test”,
<ng-container *ngFor=“let herod of heroes$.cont_act; | async as heroes; index as i; let last = last”>
- Assistant
The JSON object you provided has a different structure than the previous examples. In this case, the data you want to display is inside the
docproperty of an object.To display the emails in the
cont_actarray usingngForand the async pipe, you need to access thedocobject first and then iterate through thecont_actarray, like this:<ng-container *ngFor="let hero of heroes$ | async; index as i; let last = last"> <ng-container *ngIf="hero.doc?.cont_act"> <ng-container *ngFor="let contact of hero.doc.cont_act; let j = index; let lastContact = last"> <p>{{ contact.emil }}</p> </ng-container> </ng-container> </ng-container>Here,
heroes$is an observable which returns an array of objects. Each object has adocproperty which contains thecont_actarray.