How to prevent scroll on cell focus when using cellNav? Chrome
Hi,
I started asking in stack overflow originally but didn't get responses. I think it might be a defect that happens in Chrome (not Firefox) on the first click, second cell clicked is fine.
I have a grid that requires ui.grid.cellNav, however the first time I click on any cell that allows focus (allowCellFocus -> default is true) an scroll event happens moving up the clicked cell out of the viewport, my grid has many rows so the user needs to scroll up to reveal the clicked cell again. It only happens the first time a cell is clicked.
I've found in ui-grid.js the code below trigering the behavior:
if (grid.cellNav.lastRowCol === null || grid.cellNav.lastRowCol.row !== newRowCol.row || grid.cellNav.lastRowCol.col !== newRowCol.col){
grid.api.cellNav.raise.navigate(newRowCol, grid.cellNav.lastRowCol);
grid.cellNav.lastRowCol = newRowCol;
}
// If i comment both lines inside the if statement, it's fine, not unexpected scroll.
Steps to replicate:
Go to http://ui-grid.info/docs/#/tutorial/202_cellnav Scroll down to reveal the sample grid of the page PARTIALLY. If you scroll all the way down the unexpected scroll will not be noticeable, since you already reached the bottom of the page. (A side note it doesn't happen clicking in age since it has allowCellFocus to false. Click on any cell of ID, Name or Address A scroll will happen if you haven't already scrolled to the bottom of the page Clicking again in any other cell doesn't trigger scroll behavior a second time. To replicate the behavior you need to reload the page.
Can I prevent this behavior, while still using cellNav? Why Firefox is fine?
Thanks in advance.
The uiGridCtrl.focus is called when the grid receives focus (after selection of the cell). The focus method sets the focus to the div with the class 'ui-grid-focuser'. This div is on the bottom of the grid so the page is scrolled to the end of the grid.
uiGridCtrl.focus = function () {
gridUtil.focus.byElement(focuser[0]); // <--- break here to test
//allow for first time grid focus
};
Is there an easy way to disable this behavior without touching the code?
If you can make sure the complete grid is visible before setting the focus.
Thanks for your feedback, we opted for removing ui.grid.cellNav. I'm closing the issue.
I'm having this issue and unfortunately we can't remove the cellNav directive, still looking for a way to prevent the containing div or page from scrolling down to the bottom. Would like to use cellNav with only part of the grid showing and not having it jump.
Removing those elements seems to cause the page or containing scrollable div to not jump, a quick test like the following seems to do it, but I'm not sure if that negatively impacts anything - it doesn't seem to here.
$timeout(() => {
$('.ui-grid-focuser').remove();
});
I had the same problem. However since I could not remove the cellNav, I went to where i build my project and replaced (from the the nodes module ui grid min js file) the focus to have prevent scroll true.. i.e. :
.pipe(replace('e&&e[0].focus()', 'e&&e[0].focus({preventScroll: true})'))
and until now it seems to be working fine (both chrome and ff). I think ideally I would like to be doing this (or something similar) only when my whole grid gets focus ... If someone has a better solution or idea I would like to hear it since I dont think this is the best that could be done.