首页 > 解决方案 > 刷新页面后Ag网格行选择丢失

问题描述

我在 Angular 7.x 中使用 ag-grid。每当刷新页面时,它都会失去突出显示的行焦点。这似乎是 ag-grid 中的一个错误。

是否有任何解决方法,例如使用参数 ID 将最后选择的行焦点设置回网格。

页面刷新前 在此处输入图像描述

页面刷新后 在此处输入图像描述

标签: angularangular-routingag-grid

解决方案


您需要将所选行存储在 localStorage 中:

window.onbeforeunload = (event) => {
  localStorage.setItem("selectedRows", JSON.stringify(this.gridOptions.api.getSelectedRows()));
};

然后在刷新并在网格中设置数据之后,以编程方式重新选择您的行:

reSelect = (): void => {
  const selectedRows = JSON.parse(localStorage.getItem("selectedRows"));

  this.gridOptions.api.forEachNode((node: RowNode, index: number) => {
     // adapt with you own unique role-id rule
     const selectNode = selectedRows.some((row) => { return row.id === node.data.id; });

     if (selectNode) {
        node.setSelected(true, false);
     }
   });
};

推荐阅读