'ag-grid: "How to scroll to last known position"?

Ag-Grid offers the method ensureIndexVisible(index, 'middle'); With that method it is easy to scroll to an choosen index. But how do I get the last known index of the scrolling position from the user?

An example: We have a table with 600 rows. The user is scrolling around - an update happens. The table changed a bit. Now I have the behaviour that the table is scrolling to the top. The user has now to scroll again to his last position.

I would like to redirect the user to hist last scrolling position: ensureIndexVidisble(USERS_LAST_KNOWN_SCROLLING_POSITION, 'middle');

In addition to information: the user does not work in the table. So I cannot save his last click on a row.

How can I achieve this?



Solution 1:[1]

Update for Release 23.1.0 (1st May 2020)+

Use immutableData=true instead of deltaRowDataMode=true


In typical AG fashion they hide this vital piece of information in their docs:

Method 3 - Delta Row Data The delta method is using the row data method above but having the property deltaRowDataMode=true.

When deltaRowDataMode is on, the grid will compare the new row data with the current row data and create a transaction object for you. The grid then executes the change as an update transaction, keeping all of the grids selections, filters etc.

Use this if you want to manage the data outside of the grid (eg in a Redux store) and then let the grid work out what changes are needed to keep the grid's version of the data up to date.

https://www.ag-grid.com/javascript-grid-data-update/#bulk-updating

This in my opinion should be a setting you always use if your rows have a unique ID (I hope they do, it's good practice to do it). Set deltaRowDataMode to true and use getRowNodeId to specify a unique id for the row.

After that your grid will update much more efficiently (only updating what is needed) and it won't jump to the top when it does as it's not re-creating every row and cell in the grid on an update.

For good measure you could also add the suppressScrollOnNewData option, though I'm not sure if it's needed if you do the above.

Solution 2:[2]

Thanks a million, to spare some time to everybody here's what you have to pass to the propery [gridOptions]. As long as your data has an unique ID you're set.

// gridOptions
{
    ...,
    deltaRowDataMode: true,
    getRowNodeId: function (data) {
        return data.id;
    },
},

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Arthur Flower