I have a table in which I show data that I get from my api. In my component .ts
I do this to be able to access the data from my service, for this I do this.
getBloques() {
this.configuracioncvService.getBloques()
.subscribe(res => {
let bloquesOrdenados = _.orderBy(res,['ordenCompleto'], ['asc'])
this.configuracioncvService.bloques = bloquesOrdenados;
console.log('BLOQUESRESTAPI', bloquesOrdenados)
})
}
In my service I have this:
getBloques() {
return this.http.get<Bloque[]>(this.URL_BLOQUES);
}
And in my html
to show this data I do this:
<table class="table" id="tabla">
<thead>
<tr class="d-flex">
<th class="col-3">Bloque</th>
<th class="col-3">Orden</th>
<th class="col-3">Guardar</th>
<th class="col-3">Ingresar a Bloque</th>
</tr>
</thead>
<tbody>
<tr class="d-flex" *ngFor="let bloque of configuracioncvService.bloques">
<td class="col-md-3">{{bloque.nombre}}</td>
<td class="col-md-3">
<mat-form-field>
<input type="number" matInput placeholder="Orden" [value]="bloque.ordenCompleto" [(ngModel)]="bloque.ordenCompleto">
</mat-form-field>
</td>
<td class="col-md-3">
<button class="btn btn-secundary" (click)="editBloque(bloque)">
<i class="material-icons">save</i>
</button>
</td>
<td class="col-md-3">
<button mat-raised-button color="primary" [routerLink]="['/bloque-completo/', bloque.nombre]">
<i class="material-icons">east</i>
</button>
</td>
</tr>
</tbody>
</table>
I show the data in the table, and in that table I have an input so that the value of the column can be edited orden
, my method to edit is this
editBloque(bloque: Bloque){
this.configuracioncvService.putBloque(bloque).subscribe(res =>{
console.log('editado', res)
});
}
Everything works correctly for me, the "problem" I have is that in my html for each object I have a button, so every time I change a data, I have to click on the button so that this data is edited, as in this image it is as I currently have.
My question is how can I make a single button that edits all the objects without having to save them one by one, as I have it now. What I am looking to get is something like this.
Excuse me for the images, it is so that I can better understand what I am trying to do. I hope you can help me or give me a guide on how to get what I want. Thanks in advance.
EDITED: For now I've been trying this on stackblitz https://stackblitz.com/edit/angular-formgroup-ngxxam?file=app%2Fapp.component.ts But I don't know how to loop through the value of form
the in the method onMy()
and do the editing .
My recommendation in this case, and if it is possible based on your requirements, is that you use
ReactiveForms
to keep the state of the form from your file more organized.ts
;Thus, you could declare the structure of your form with a
FormGroup
orFormArray
if it is dynamic, you assign the controls to the template, and in your code you can always access the current value of the form control; after that it's a matter of taking the generated object and saving it on the final button click.A simple example of this structure would be something like this:
The same can be easily achieved by using the
ngModel
and binding each input to a property of the object..html
.ts
show