以下是示例代码:
HTML 模板:
{{ item.name }}
{{ item.age }}
{{ item.email }}
TypeScript 代码:
export class MyComponent {
items: any[];
openIndex: number;
constructor() {
this.items = [
{name: 'Alice', age: 25, email: 'alice@example.com'},
{name: 'Bob', age: 30, email: 'bob@example.com'},
{name: 'Charlie', age: 35, email: 'charlie@example.com'}
];
this.openIndex = -1;
}
toggleRow(index: number) {
if (index === this.openIndex) {
// 点击已打开的行,关闭它
this.openIndex = -1;
} else {
// 关闭当前已打开的行,打开当前点击的行
this.openIndex = index;
}
}
}
CSS 样式:
tr.expanded {
display: table-row;
}