A Visual Studio 2019 project with an Angular 12 UI.
All of the code was originally in CompA of the UI, specifically there was a mat-table:
<table mat-table #myTable>
as well as a click event on the table rows, so that when a row is clicked, its background color changed to yellow:
...
<mat-row *matRowDef="let row; columns: myColumns;" (click)="setRowColor($event)"></mat-row>
</table>
In the ts file, I have a ViewChild to the table:
@ViewChild('myTable', { static: true }) myTableVar: MatTable<any>
plus code to handle setting/clearing the background color:
setRowColor(ev: Event) {
let el: HTMLElement = ev.currentTarget as HTMLElement;
if (el.style.backgroundColor === 'yellow')
el.style.backgroundColor = '';
else
el.style.backgroundColor = 'yellow';
}
There is also a button on CompA that when clicked, will cycle through all rows in the table, processing any row that had a background color of yellow, and then clearing the background color for the row:
processRowsInMatTable() {
const el: HTMLElement[] = this.myTableVar._getRenderedRows(this.myTableVar._rowOutlet);
el.forEach(e => {
const eHtml: HTMLElement = e as HTMLElement;
if (eHtml.style.backgroundColor === 'yellow') {
//some processing;
eHtml.style.backgroundColor = '';
}
});
}
Everything was working as expected. However as CompA was getting too large, CompB was created, and the button that would cycle through all the rows in the mat-table is now in CompB. How can I get a reference to CompA.myTable in CompB, so that I can perform the logic in method processRowsInMatTable (which is also in CompB now)? Also, there is no parent/child relationship between A and B.