我有一个包含几列的表,其中之一是输入。在表格下我有一个按钮,点击它我想更新数据库,一键更新所有用户的年龄。因此,对于通话,我需要每个用户的行 ID 和输入值。谁能解释一下怎么做?
TS
const ELEMENT_DATA: User[] = [
{ id: 1, country: 'UK', name: 'A', age: 20 },
{ id: 2, country: 'France', name: 'B', age: 21 },
{ id: 3, country: 'Germany', name: 'C', age: 22 }
];
export interface User {
id: number;
country: string;
name: string;
age: number;
}
@Component({
selector: 'app-table',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css']
})
export class TableComponent implements OnInit {
displayedColumns = ['country', 'name', 'age'];
dataSource = ELEMENT_DATA;
ngOnInit() {
}
}
HTML
<div>
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<ng-container matColumnDef="country">
<th mat-header-cell *matHeaderCellDef> Country </th>
<td mat-cell *matCellDef="let element"> {{element.country}} </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<ng-container matColumnDef="age">
<th mat-header-cell *matHeaderCellDef> Age </th>
<td mat-cell *matCellDef="let element">
<div>
<input type="text" class="age-input" value="{{element.age}}">
</div>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<button mat-raised-button type="submit">
<i class="material-icons mr-2">save</i>Save
</button>
</div>