我正在使用AG grid Angular创建一个日志视图,我将每秒向网格中添加许多条目,通常这种类型的视图底部有最新的条目,所以如果有一种好的方法可以让滚动停留在底部,这样我总是可以看到最新的条目,那就太好了。一个奖励将是,如果我可以手动向上滚动,滚动将停留在这个位置。
发布于 2019-04-19 23:03:00
这里有一种方法:
在标记中处理rowDataChanged
:
<ag-grid-angular
class="ag-dark"
[rowData]="messages"
[columnDefs]="columnDefs"
[gridOptions]="gridOptions"
(bodyScroll)="handleScroll($event)"
(rowDataChanged)="handleRowDataChanged($event)">
</ag-grid-angular>
在rowDataChanged
的处理程序中,调用ensureVisibleIndex
滚动到添加的最后一行:
gridOptions: GridOptions = {
suppressScrollOnNewData: true,
}
handleRowDataChanged(event) {
const index = this.messages.length - 1;
this.gridOptions.api.ensureIndexVisible(index, 'bottom');
}
演示:https://stackblitz.com/edit/angular-ag-grid-stuck-to-bottom
在该代码中,还存在一些逻辑,即何时根据滚动位置将表格滚动到末尾。
https://stackoverflow.com/questions/55328287
复制相似问题