首先,我们需要在组件中定义一个变量来存储当前编辑的行的值。接下来,在表格中的每一行中,我们可以添加一个“edit”按钮,并使用该按钮的点击事件将该行的值存储在定义的变量中。最后,我们可以根据存储在该变量中的值在表格下方的表单中预填充该行的编辑值。
以下是代码示例:
组件:
import { Component } from '@angular/core';
@Component({
selector: 'app-table',
template: `
ID
Name
Action
{{ row.id }}
{{ row.name }}
`
})
export class TableComponent {
data = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Bob' }
];
isEditing = false;
currentRowValue = {};
editRow(row) {
this.isEditing = true;
this.currentRowValue = { ...row };
}
submitForm() {
// Save the edited row
this.data = this.data.map(row => {
if (row.id === this.currentRowValue.id) {
return { ...this.currentRowValue };