简体   繁体   English

如何在无限行模式下在 ag-grid 上 select 一行

[英]How to select a row on ag-grid in Infinite Row mode

Many similar questions here, but I'm unable to find a solution to my basic requirement.这里有很多类似的问题,但我无法找到我的基本要求的解决方案。

I'm using ag-grid in React in the Infinite Row model.我在 Infinite Row model 的 React 中使用 ag-grid。 Data are loaded from server by blocks of 100.数据按 100 个块从服务器加载。

I would like to preselect a row - for example row id 500. This requires loading the right block and, once loaded, then selecting the right node.我想预先选择一行 - 例如行 id 500。这需要加载正确的块,一旦加载,然后选择正确的节点。

Example here: (click on Jump to 500 button) demo code此处示例:(单击跳转到 500按钮)演示代码

const jumpTo500 = () => {
  if (gridApi.getInfiniteRowCount() < 501) {
    gridApi.setRowCount(501, false);
  }
  gridApi.ensureIndexVisible(500, 'middle');

  gridApi.forEachNode((node) => {
    if (node.rowIndex === 499) {
      node.setSelected(true);
    }
  });
};

Right now I need to click twice on the button because I try to select to row before the node is loaded.现在我需要在按钮上单击两次,因为我尝试在加载节点之前将 select 排到行。

Question: How to know when the grid is rendered and the new nodes are loaded?问题:如何知道何时渲染网格和加载新节点?

onRowDataChanged() and onRowDataUpdated() events are never called.永远不会调用onRowDataChanged()onRowDataUpdated()事件。

my current workaround...我目前的解决方法...

    const jumpTo500 = async () => {
      if (!gridApi) return;

      const node = gridApi.getModel().getRow(500);
      if (node) {
        until(() => !!node.id, 50, 5000)
            .then((ms) => {
                console.log("ok in ", ms, "ms");
                node.setSelected(true, false, true);
                gridApi.ensureNodeVisible(node, "middle");
            })
            .catch((ms) => console.log("timeout after", ms, "ms"));
      }
  };

I'm able to select the row once the node.id is available.一旦 node.id 可用,我就可以 select 行。 Utility method until() will retry every 50ms until the condition is ok or if timeout after 5sec.实用方法 until() 将每 50 毫秒重试一次,直到条件正常或 5 秒后超时。

    const until = (condition: () => boolean, interval: number, timeout: number) =>
      new Promise<number>((resolve, reject) => {
        if (condition()) {
            resolve(0);
        }
        const start = new Date().getTime();
        const intervalTimer = setInterval(() => {
            const elapsed = new Date().getTime() - start;
            if (condition()) {
                clearInterval(intervalTimer);
                resolve(elapsed);
            } else {
                if (elapsed >= timeout) {
                    clearInterval(intervalTimer);
                    reject(elapsed);
                }
            }
        }, interval);
    });

In createDataSource, after setting the data, call callback.在createDataSource中,设置好数据后,调用callback。

Code in onGridReady: onGridReady 中的代码:

    const calllbackAfterSetData = (): boolean => {
        const defIndex = localeSaver.loadObjectOrDefault<RowClicked>({ [rowClicked]: null })
        const infiniteRow = params.api.getInfiniteRowCount()

        if (!defIndex || defIndex.rowIndex === -1 || !infiniteRow) return false

        if (infiniteRow < (defIndex.rowIndex + 1)) {
            params.api.setRowCount(defIndex.rowIndex + 1, false)
        }
        params.api.ensureIndexVisible(defIndex.rowIndex, "top") 

        const node = params.api.getRowNode(defIndex.nodeId)

        if (node) {
            node?.setSelected(true)
            localeSaver.clear({ [rowClicked]: null })

            return true
        } else return false
    }

    const dataSource: IDatasource = dataSource(params.api, params.columnApi, calllbackAfterSetData)
    params.api.setDatasource(dataSource)

Code in createDataSource: createDataSource 中的代码:

function dataSource( gridApi: GridApi, columnApi: ColumnApi, calllbackAfterSetData?: () => boolean) {
    return {
        getRows: async (params: IGetRowsParams) => {

            const response: string[] = []
            // any fetch
            if (response) {
                params.successCallback(response, 100)

                if (calllbackAfterSetData) {
                    const result = calllbackAfterSetData()
                    if (result) calllbackAfterSetData = undefined
                }

            } else {
                params.failCallback()
            }
        },


        destroy: () => {

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM