I'm using @swimlane/ngx-datatable
to display data from the server. I poll these data, so every 1 min. I got a new set of data. My problem is, if the user selects a row in the data table after the polling the data gets rewritten by new data and the selection gets lost. To demonstrate it I created a dummy example on stackblitz.
- Wait till data gets displayed in the table
- Select a row
- Wait till new data arrives (4s, setInterval)
- The selection is cleared
Any idea how to solve this?
app.component.ts
import { Component, VERSION, OnInit } from "@angular/core";
import { ColumnMode, SelectionType } from "@swimlane/ngx-datatable";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
SelectionType = SelectionType;
rows = [];
columns = [{ prop: "name" }, { name: "Gender" }, { name: "Company" }];
selected = [];
onSelect({ selected }) {}
ngOnInit() {
setInterval(() => {
this.rows = [
{ name: "Austin", gender: "Male", company: "Swimlane" },
{ name: "Dany", gender: "Male", company: "KFC" },
{ name: "Molly", gender: "Female", company: "Burger King" }
];
}, 4000);
}
}
app.component.html
<div>
<ngx-datatable class="material" [rows]="rows" [columns]="columns" [selected]="selected"
[selectionType]="SelectionType.single" (select)="onSelect($event)">
</ngx-datatable>
</div>
question from:
https://stackoverflow.com/questions/65922895/new-data-clears-selection 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…