1

我在对话框中有一个 igx 网格。我使用数组posts作为网格的数据。这个数组的值可以通过程序改变。如何使用数组的最后一个值更新网格数据posts

<igx-dialog #dialog>
  <igx-grid #grid1
            [emptyGridMessage]="'The grid is empty'"
            width="1200px"
            [data]="posts"
            height="600px"
            style="margin: auto"
            [primaryKey]="'person'">
    <igx-column field="person" dataType="string" header="PersonId"></igx-column>
    <igx-column field="name" dataType="string" header="Name"></igx-column>
  </igx-grid>
</igx-dialog>

PS我正在使用C#。

4

2 回答 2

1

如果您只是将新项目推送到基础数据,请记住这不会更改其引用。对于立即刷新,可以使用网格 API 方法添加行,而不是直接推送到数据源。当推送到数据源时,网格将在下一个变化检测生命周期刷新,或者数据数组可以像这样改变:

this.data = [...this.data, newItem]; // instead of push

这是必需的,因为网格使用 OnPush 策略并且不会被通知更改(因为数据数组是相同的),所以它会在下次触发更改检测时更新,或者在 Konstantin 建议的情况下,通过调用 grid.markForCheck()。

于 2021-11-15T12:30:12.460 回答
1

当组件端的数据发生变化时,您可以调用markForCheck()网格,这将强制网格运行更改检测并显示更新的数据。

示例component.ts

@ViewChild('grid1')
public grid: IgxGridComponent;

public dataChanged() {
  // logic for posts to be assigned a new value
  this.grid.markForCheck();
}
于 2021-09-10T15:29:34.427 回答