Can someone explain how to showcase a collection attribute in Angular?

I am working with a collection of objects called "ELEMENT_DATA". Each object in the collection has an attribute named "Activite", which is also a collection. My goal is to display this attribute in a specific way.

Below is my app.component.ts:

export class AppComponent {

ELEMENT_DATA: Patient[] = [
    {
        Nom_Prenom: 'Badre Labiad',
        Ne_le: new Date(1991, 9, 18),
        Activite: [{"name": 'PPC'}],
        Obs: '6h48/ 30j',
        IAH: 'IAH 6',
        Fuites: 'Fuites : 12',
        Renouvellement: new Date(1991, 9, 18),
        Valider_AR: false
    },
 .
 .
 .
];

} here is my app.component.html :

<table id="fileActive" mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">

<ng-container matColumnDef="Nom_Prenom">
    <th class="nom sortable" *matHeaderCellDef mat-sort-header>
        <div>Nom Prénom</div>
    </th>
    <td class="nom" mat-cell *matCellDef="let element"> {{element.Nom_Prenom}} </td>
</ng-container>


<ng-container matColumnDef="Ne_le">
    <th class="naissance sortable date" *matHeaderCellDef mat-sort-header>
        <div> Né(e)le</div>
    </th>
    <td mat-cell *matCellDef="let element"> {{element.Ne_le | date }} </td>
</ng-container>


<ng-container matColumnDef="Activite">
    <th class="act" *matHeaderCellDef>
        <div>Activités prescites</div>
    </th>
    <tr *ngFor="let a of ELEMENT_DATA">
    <td mat-cell *ngFor="let b of a.Activite">
            <span>
                {{b.name}}
            </span>
    </td>
    </tr>
</ng-container>
 .
 .
 .

I have tried multiple solutions but haven't found success yet. If anyone can provide a solution, I would greatly appreciate it. Thank you in advance.

Answer №1

Reposition the *ngFor directive to an HTML element nested inside of the <td>.

<ng-container matColumnDef="Activite">
    <th class="act" *matHeaderCellDef>
        <div>Prescribed Activities</div>
    </th>
    <tr *ngFor="let a of ELEMENT_DATA">
    <td mat-cell >
            <div *ngFor="let b of a.Activite">
                {{b.name}}
            </div>
    </td>
    </tr>
</ng-container>

Answer №2

to tackle the issue at hand, I decided to modify the type of activity:

Activity: Array<string>

Therefore, I made these changes in :

app.component.ts :

export class AppComponent {




ELEMENT_DATA: Patient[] = [
    {
        Name: 'Badre Labiad',
        Date_of_Birth: new Date(1991, 9, 18),
        Activity: ['O2'],
        Notes: '6h48/ 30d',
        AHI: 'AHI 6',
        Leaks: 'Leaks : 12',
        Renewal: new Date(1991, 9, 18),
        Validate_AR: false
    },
    {
        Name: 'Othman',
        Date_of_Birth: new Date(1985, 5, 20),
        Activity: ['O2'],
        Notes: '6h48/ 30d',
        AHI: 'AHI 6',
        Leaks: 'Leaks : 12',
        Renewal: new Date(2021, 9, 18),
        Validate_AR: true
    },
    {
        Name: 'Youness',
        Date_of_Birth: new Date(1984, 9, 18),
        Activity: ['Aidt'],
        Notes: '6h48/ 30d',
        AHI: 'AHI 6',
        Leaks: 'Leaks : 12',
        Renewal: new Date(2021, 9, 18),
        Validate_AR: true
    },
    {
        Name: 'Anouar',
        Date_of_Birth: new Date(1996, 7, 10),
        Activity: ['Vent' , 'O2',  'PPC', 'Aéro', 'Aidt'],
        Notes: '6h48/ 30d',
        AHI: 'AHI 6',
        Leaks: 'Leaks : 12',
        Renewal: new Date(2021, 9, 18),
        Validate_AR: false
    },
    {
        Name: 'Oussama',
        Date_of_Birth: new Date(1989, 9, 8),
        Activity: ['PPC', 'Aéro', 'Aidt'],
        Notes: '6h48/ 30d',
        AHI: 'AHI 6',
        Leaks: 'Leaks : 12',
        Renewal: new Date(2021, 9, 18),
        Validate_AR: true
    },
    {
        Name: 'Badr',
        Date_of_Birth: new Date(1989, 9, 18),
        Activity: [ 'Vent','O2','PPC' ],
        Notes: 'Obs: 6h48/ 30d',
        AHI: 'AHI 6',
        Leaks: 'Leaks : 12',
        Renewal: new Date(2021, 9, 18),
        Validate_AR: false
    },
    {
        Name: 'Abdilah',
        Date_of_Birth: new Date(1991, 8, 28),
        Activity: [ 'Vent', 'Aidt','NUT'],
        Notes: 'Obs: 6h48/ 30d',
        AHI: 'AHI 6',
        Leaks: 'Leaks : 12',
        Renewal: new Date(2021, 9, 18),
        Validate_AR: true
    },
];

}

and in my html file, I implemented the following:

<ng-container matColumnDef="Activity">
    <th class="act" *matHeaderCellDef>
        <div>Prescribed Activities</div>
    </th>

    <td class="a" mat-cell *matCellDef="let element">
        <div *ngFor="let a of element.Activity">
            {{a}}
        </div>
    </td>
</ng-container>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Extended Date type is indicating that the function cannot be located

I came across this helpful gist with date extensions: https://gist.github.com/weslleih/b6e7628416a052963349494747aed659 However, when attempting to use the functions, I encountered a runtime error stating: TypeError: x.isToday is not a function I foun ...

Tips for parsing data arrays in HTML templates

I have three variables and I created an array where I pushed all these three variables in. In my HTML template, I am using a table. I tried using *ngFor but it is not working, and also attempted string interpolation which also did not work. Currently, I ...

Unlock the power of Angular pipes in window.open with URL parameters!

<div class="member-img" onclick="window.open(childEpisode.File_URL | fullPath)"> </div> The fullPath function concatenates the domain part to the relative URL stored in file_URL. However, there seems to be an issue as it i ...

Angular does not adhere to globally defined styles

I have defined the margins for mat-expansion-panel in my styles.css file as follows: mat-expansion-panel { margin: 2vh; } Unfortunately, these margins will not be applied to my components unless they are also specified in the local CSS file. Even try ...

"Enhance your development experience with the TypeScript definitions for the Vue 2 plugin

Currently, I am utilizing VSCode alongside TypeScript classes for developing Vue 2 components. You can check out more information at: vuejs/vue-class-component. Within my present project, I make use of plugins like vue-i18n for handling translations of la ...

Center the mat-icon within a label

I'm seeking assistance with aligning a mat-icon beside text within a label Here is the HTML code snippet: <label> <mat-icon>warning</mat-icon> Warning </label> And here is the corresponding CSS code: label { font- ...

Acquire information from the user interface and display it in a highcharts chart using Angular 5

Currently, I am utilizing an npm package for chart creation, which can be found at the following link: https://www.npmjs.com/package/angular-highcharts I have a specific interface set up and I aim to extract values from this interface to populate a line g ...

Exploring SubjectBehavior within my UserService and Profile Component

Within my ShareService, I have the following code snippet: isLogin$:BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false); <==== default value is false CheckUser = this.isLogin$.asObservable(); public isLogin (bool){ ...

Dynamic forms in Angular do not currently have validation support for forms with multiple parts

I am currently working on creating a dynamic form with multi-part answers using Angular's Dynamic Form example (https://angular.io/guide/dynamic-form) and live example (https://angular.io/generated/live-examples/dynamic-form/eplnkr.html). I have set u ...

Please refresh the page to view the updated component

Within my Angular 7 application, I have various components that retrieve data from an API by making a call in the ngOnInit function. So far, the CRUD operations are functioning correctly and I am able to obtain the desired results. However, the main issue ...

Is there a way to use Regex to strip the Authorization header from the logging output

After a recent discovery, I have come to realize that we are inadvertently logging the Authorization headers in our production log drain. Here is an example of what the output looks like: {"response":{"status":"rejected",&quo ...

Develop an interface in TypeScript for intricate data structures

Displayed below is a variable that contains a collection of objects: scenes = { sky: { image: 'assets/1.jpg', points: { blue_area: { x: 1, y: 2 }, } }, blue_area: { image: & ...

Tips for implementing a delay in HTTP requests using RxJS 6.3.0

When I try to use delay with the HTTPClient object, it gives me the following error: Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures. TypeScript Concerns: import { delay } from & ...

Struggling to track down the issue in my ts-node express project (Breakpoint being ignored due to generated code not being located)

For my current project, I decided to use the express-typescript-starter. However, when I attempted to debug using breakpoints in VS Code, I encountered an issue where it displayed a message saying "Breakpoint ignored because generated code not found (sourc ...

Challenge encountered while populating dropdown in Angular reactive form

When using template-driven forms, I was able to populate dropdowns. However, now that I'm using material reactive form, I am unable to do so. My goal is to populate the "country" dropdown and then call an "onstatechange" event later on to populate the ...

Organize Dates in React Table

I need help with sorting the Date column in my code. Currently, the sorting is being done alphabetically. Here is the JSON data and code snippet: JSON [ { "date": "Jun-2022" }, { "date": "Jul-2022" } ...

Is it recommended to store SCSS and TypeScript files in the wwwroot folder in ASP.NET Core and Angular 2 development?

I'm struggling to understand where I should place my ts and scss files. The wwwroot folder is specifically for static files, but the ts and scss files are compiled. Should I recreate a similar folder structure both in wwwroot and outside of it, and th ...

Utilize Angular 2 with Google Maps Places Autocomplete to dynamically update an input field with location suggestions [DOM manipulation]

Recently, I have been utilizing the "Angular 2 + Google Maps Places Autocomplete" search functionality. Essentially, it involves an input field that looks like this: <input placeholder="search your location" autocorrect="off" autocapitalize="off" spell ...

Receiving the [object HTMLInputElement] on the screen rather than a numerical value

I have an input box where a user can enter a number. When they click a button, I want that number to be displayed on the page. However, instead of seeing the number, I am getting the output as [object HTMLInputElement]. Below is my TypeScript code: let qu ...

What is the best approach to managing a 204 status in Typescript in conjunction with the Fetch API

Struggling to handle a 204 status response in my post request using fetch and typescript. I've attempted to return a promise with a null value, but it's not working as expected. postRequest = async <T>(url: string, body: any): Promise ...