我在这里分叉了你的代码。在更新行时保留扩展行的修改代码是
import { Component, ViewChild, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
@ViewChild('myTable') table: any;
// to hold expanded row IDs
expandedId=[];
rows = [
{ id: 0, name: 'Austin', gender: 'Male', age: 20},
{ id: 1, name: 'Dany', gender: 'Male', age: 30},
{ id: 2, name: 'Molly', gender: 'Female', age: 40},
];
constructor() {}
toggleExpandRow(row) {
console.log('Toggled Expand Row!', row);
this.table.rowDetail.toggleExpandRow(row);
// adding the expanded row to row id on expanding and removing row id on collapse.
const index=this.expandedId.indexOf(row.id);
if(index > -1)
this.expandedId.splice(index,1);
else{
this.expandedId.push(row.id);
}
}
onDetailToggle(event) {
console.log('Detail Toggled', event);
}
updateTable(){
this.rows[0].age = this.rows[0].age + 1;
this.rows = [...this.rows];
// expanding the rows upon updating the row values.
setTimeout(()=> {
this.expand();
},100);
}
// method to toggle the rows in expandedId
expand(){
this.table.rowDetail.collapseAllRows();
for(var i=0;i<this.rows.length;i++){
if(this.expandedId.indexOf(this.rows[i].id) > -1){
this.table.rowDetail.toggleExpandRow(this.rows[i])
}
}
}
ngOnInit(){
}
}