I wrapped an ngx-datatable component in a form
tag so I can validate inputs
in the table cells. Due to the nature of how the table is populated I set the inputs name
properties dynamically
<form #tableForm="ngForm">
<ngx-datatable
[rows]="_rows">
<ng-container *ngFor="let column of rowDeffinition; let columnIndex=index">
<ngx-datatable-column [prop]="column.key" [name]="column.label">
<ng-template ngx-datatable-cell-template let-rowIndex="rowIndex" let-value="value" let-row="row">
<input
class="cell-input"
(blur)="updateCellValue($event, column.key, rowIndex)"
type="text"
[ngModel]="value"
[name]="rowIndex + '-' + column.key"
/>
...
</ng-template>
</ngx-datatable-column>
</ng-container>
</ngx-datatable>
</form>
Normally, the name
property would creates a local variable in the template and you can access the inputs control properties via the variable name.
<input type="text" name="name" [(ngModel)]="name" required minlength="4" />
<div *ngIf="name.invalid && name.touched">
I wonder how can I do this dynamically in the same manner I set the inputs name. So far I was able to access the input controls via the form reference but this becomes quite wordy
<span *ngIf="!tableForm.controls[rowIndex + '-' + column.key]?.valid &&
!tableForm.controls[rowIndex + '-' + column.key]?.pristine"
class="[ c-validation-message ]">
required
</span>