Hi
Below is the data returned.I am trying below Html. I want to display only Name & Email.
In List Data there are more than 2 columns like Id,Name,Email,Contact No. etc.
Code
{{ column }}
{{ element[column] }}TypeScript
@Input() arrChildHead: string[] = [];
@Input() lstChild: any[] = [];
In arrChildHead it has 2 fields Name,Email.
In Lstchild it is data which is returned through API.Thanks

Sigar DaveRank #676 · 3.6K reputationPosted Jul 3, 2024, 7:31 AM EDT
To display only the "Name" and "Email" columns from your data using Angular and Angular Material, you need to make sure that
arrChildHeadcontains only those two columns and that yourdisplayedColumnsarray is set accordingly. Here's a step-by-step solution:displayedColumnsarray:Ensure that
displayedColumnsonly includes "Name" and "Email".Modify the
*ngFordirective to iterate over thedisplayedColumnsarray and bind data correctly.Here's how you can structure your component:
And your component TypeScript file should look like this:
import { Component, Input } from '@angular/core'; @Component({ selector: 'app-child-component', templateUrl: './child-component.html' }) export class ChildComponent { @Input() arrChildHead: string[] = ['Name', 'Email']; // Specify the columns you want to display @Input() lstChild: any[] = []; // Your data array displayedColumns: string[] = this.arrChildHead; // Set the displayed columns }With this setup:
arrChildHeadshould only include the column names "Name" and "Email".displayedColumnswill take the values fromarrChildHead.The HTML template iterates over
displayedColumnsand displays only the "Name" and "Email" columns from your data.Make sure that your data in
lstChildcontains the corresponding fields "Name" and "Email". For example, your data array might look something like this:lstChild = [ { Id: 1, Name: 'John Doe', Email: '[email protected]', ContactNo: '1234567890' }, { Id: 2, Name: 'Jane Smith', Email: '[email protected]', ContactNo: '0987654321' } ];With this configuration, only the "Name" and "Email" columns will be displayed in the table.
Ramco RamcoRank #462 · 5.4K reputationPosted Jul 3, 2024, 7:43 AM EDT
Hi Dave
I am getting this error. Below is the TypeScript
The label's
forattribute doesn't match any elementid. This might prevent the browser from correctly autofilling the form and accessibility tools from working correctly.To fix this issue, make sure the label's
forattribute references the correctidof a form field.export class DynamicMatTableComponent implements OnInit,AfterViewInit,OnChanges { @Input() title?: string; @Input() arrChildHead: string[] = ['Name', 'Email']; @Input() lstChild: any[] = []; @Input() isAction: boolean = false; @Input() columns: any = []; dataSource: MatTableDataSource; displayedColumns: string[] = []; @Output() getTableData: EventEmitter = new EventEmitter(); @ViewChild(MatPaginator, { static: true }) paginator!: MatPaginator; @ViewChild(MatSort) sort!: MatSort; @Output() pageChange = new EventEmitter(); flg?: boolean = true; isLoading = true; pageSizeOptions: number[] = []; showMessage:string=''; @Input() endpointurl?:string; constructor(private cdr: ChangeDetectorRef) { this.dataSource = new MatTableDataSource([]); this.paginator = {} as MatPaginator; } defaultImage:string='assets/images/icons/no-photo.png'; ngOnInit(): void { //this.pageSizeOptions = this._common.getPageSizeOptions(); this.displayedColumns = this.getDisplayedColumns(); } ngOnChanges(changes:SimpleChanges){ this.isLoading = true; if (changes?.['lstChild'] && changes?.['lstChild'].currentValue) { this.dataSource = new MatTableDataSource(this.lstChild); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; this.isLoading = false; console.log(this.lstChild); console.log(this.arrChildHead); } this.displayedColumns = this.getDisplayedColumns(); this.cdr.detectChanges(); } ngAfterViewInit(){ } getDisplayedColumns(): string[] { this.columns = this.arrChildHead; return this.columns.filter((column:any)=>column.visible).map((column:any)=>column.name); } getColumnTitle(columnName: string): string { const column = this.columns.find((col:any) => col.name === columnName); return column ? column.title : ''; } applyFilter(event: Event) { const filterValue = (event.target as HTMLInputElement).value; this.dataSource.filter = filterValue.trim().toLowerCase(); } btnHardDelete=($event:any,record:any)=>{ } }Thanks
Sigar DaveRank #676 · 3.6K reputationPosted Jul 3, 2024, 7:55 AM EDT
Please ensure the
idandforattributes are set correctly for each iteration.something like this