首页 > 解决方案 > 新数据清除选择

问题描述

@swimlane/ngx-datatable用来显示来自服务器的数据。我轮询这些数据,所以每 1 分钟一次。我得到了一组新数据。我的问题是,如果用户在轮询后选择数据表中的一行,则数据会被新数据重写并且选择会丢失。为了演示它,我在stackblitz创建了一个虚拟示例。

在此处输入图像描述

知道如何解决这个问题吗?

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>

标签: angularngx-datatable

解决方案


推荐阅读