Hi
I have below array for Header information . I want to give proper Headings to table columns. What changes need to be done in Html
C#
arrheader = [
{'Head': 'Category','FieldName': 'categoryName'},
{'Head': 'Name','FieldName': 'productName'},
{'Head': 'Description','FieldName': 'productDescription'},
{'Head': 'Price','FieldName': 'productPrice'}
]TypeScript
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-dynamic-table',
template: `
{{ header }}
{{ element[header] }}
`,
})
export class DynamicTableComponent {
@Input() arrHeader: any = [];
@Input() arrGrid: any = [];
}
Thanks

Prasad RaveendranRank #320 · 7.8K reputationPosted Jun 24, 2024, 6:58 PM EDT
Using standalone components in Angular, you can create a more modular and isolated setup without the need for an
Step 1: Create the Standalone ComponentNgModule. Here's how you can achieve the dynamic table using Angular standalone components in version 17.First, create a standalone component for your dynamic table. This component will have the same logic and template as before but will be defined as a standalone component.
dynamic-table.component.ts
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core'; import { MatTableModule } from '@angular/material/table'; @Component({ selector: 'app-dynamic-table', standalone: true, imports: [MatTableModule], template: ` {{ header.Head }} {{ element[header.FieldName] }} `, }) export class DynamicTableComponent implements OnChanges { @Input() arrHeader: any = []; @Input() arrGrid: any = []; displayedColumns: string[] = []; ngOnChanges(changes: SimpleChanges) { if (changes.arrHeader) { this.displayedColumns = this.arrHeader.map((header: any) => header.FieldName); } } }app.component.ts
import { Component } from '@angular/core'; import { DynamicTableComponent } from './dynamic-table.component'; @Component({ selector: 'app-root', standalone: true, imports: [DynamicTableComponent], template: ` `, }) export class AppComponent { arrHeader = [ { Head: 'Category', FieldName: 'categoryName' }, { Head: 'Name', FieldName: 'productName' }, { Head: 'Description', FieldName: 'productDescription' }, { Head: 'Price', FieldName: 'productPrice' } ]; arrGrid = [ { categoryName: 'Electronics', productName: 'Smartphone', productDescription: 'Latest model', productPrice: '$699' }, { categoryName: 'Home Appliances', productName: 'Washing Machine', productDescription: 'Top Load', productPrice: '$499' } ]; }main.ts
import { bootstrapApplication } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { provideAnimations } from '@angular/platform-browser/animations'; bootstrapApplication(AppComponent, { providers: [provideAnimations()] }).catch(err => console.error(err));Standalone Dynamic Table Component:
standalone: true.MatTableModuledirectly in the component.Standalone App Component:
DynamicTableComponentdirectly without needing anNgModule.Bootstrap Application:
bootstrapApplicationto bootstrap theAppComponent.This approach leverages Angular's standalone component features to create a more modular and isolated setup, allowing for easy maintenance and scalability.
Jithu ThomasRank #437 · 5.7K reputationPosted Jun 24, 2024, 3:58 PM EDT
import { Component, Input, OnInit } from '@angular/core'; @Component({ selector: 'app-dynamic-table', templateUrl: './dynamic-table.component.html', }) export class DynamicTableComponent implements OnInit { @Input() arrHeader: any = []; @Input() arrGrid: any = []; displayedColumns: string[] = []; ngOnInit() { this.displayedColumns = this.arrHeader.map(header => header.FieldName); } }This is a basic implementation, make changes and test.
Regards,