Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

angular - New data clears selection

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.

enter image description here

  • 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...