当数据改变时重新加载igx网格



我在一个对话框中有一个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>

注:我正在使用c#。

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

示例component.ts:

@ViewChild('grid1')
public grid: IgxGridComponent;
public dataChanged() {
// logic for posts to be assigned a new value
this.grid.markForCheck();
}

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

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

这是需要的,因为网格使用OnPush策略,不会被通知变化(因为数据数组是相同的),所以它会更新下一次触发变化检测,或者在康斯坦丁建议的情况下,通过调用grid. markforcheck()。

最新更新