Disclaimer, this is my first Angular project. So bare with me.
What I want is multiple tables with inputs to change their number of rows based on a drop down menu:
When the drop down circled in red is changed, the number of rows in each of the tables will change.
The drop down menu is in one component. Each table is its own component (app-input-tables) used four times. It is set up like this:
<select class = "dropDown" #types (change)="onSelected(types.value)">
options...
</select>
</div>
<div><app-input-table></app-input-table></div>
<div><app-input-table></app-input-table></div>
<div><app-input-table></app-input-table></div>
<div><app-input-table></app-input-table></div>
The "onSelected" method looks like this:
inputs1: InputTableComponent = new InputTableComponent()
inputs2: InputTableComponent = new InputTableComponent()
inputs3: InputTableComponent = new InputTableComponent()
inputs4: InputTableComponent = new InputTableComponent()
onSelected(value:string): void {
this.inputs1.typeEntry(value);
this.inputs2.typeEntry(value);
this.inputs3.typeEntry(value);
this.inputs4.typeEntry(value);
}
The app-input-table html looks like this:
<div>
<label>SINT</label>
<table>
<tr *ngFor="let row of inputs; let i = index; trackBy:trackByFn">
<td> <input class = "inputValues" [(ngModel)]="inputs[i]" (keyup.tab)="valueEntry()" /></td>
</tr>
</table>
</div>
My trackByFn and typeEntry methods look like this:
trackByFn(index: any, item: any){
return index;
}
typeEntry(selectedType: string) {
if (selectedType == "BOOL")
this.inputs = Array(8).fill(0);
else
this.inputs = Array(1).fill(0);
this.changeDetection.detectChanges()
}
So I want the *ngFor to update when the drop down menu changes.
So I tried to follow the instructions here:
I've been trying to follow the "ChangeDetectorRef" suggestion given in the answers. If I do the following:
constructor(
private changeDetection: ChangeDetectorRef
) { }
I get the following compile time error: "An argument for 'changeDetection' was not provided."
If I move the changeDetection parameter from the constructor to a private field:
export class InputTableComponent implements OnInit {
inputs: number[] = [];
private changeDetection: ChangeDetectorRef
I do not get the compile time error. But I then get a runtime error when it it reads "this.changeDetection.detectChanges()" line:
typeEntry(selectedType: string) {
if (selectedType == "BOOL")
this.inputs = Array(8).fill(0);
else
this.inputs = Array(1).fill(0);
this.changeDetection.detectChanges()
}
The error is: "Cannot read properties of undefined (reading 'detectChanges')"
I'm not sure what I'm doing wrong. I have imported the ChangeDetectorRef:
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit} from '@angular/core';
I have also tried the ChangeDetectionStrategy.Default. Which was another answer on that page, but that didn't help either.
Any help would be great. Thanks.