Search code examples
extjsextjs3

How do you stop scrolling to top when grid store is reloaded - take 2


I have an app that uses ExtJS 3.3.0. It uses an EditorGridPanel in which after the store is reloaded - I would like it to preserve the scroll position of the grid rather than sending it back up to the top.

Since it's an earlier version of ExtJS - this does not work.

viewConfig: {
   preserveScrollOnRefresh: true
}

I thought I had found a solution per Zeke's suggestion to use a scrollTo function like so:

//Save position
var top = grid.getView().scrollergetScroll().top;

//Restore position
grid.getView().scroller.scrollTo('top',top);

This works great - except that after it's done reloading it goes right back up to the top. Basically this would be a perfect solution if only I didn't need preserve the cursor position after reloading the store.

grid().getStore().reload({
    callback: function (response) {
        //Works at this point
        grid.getView().scroller.scrollTo('top',top);
    }
}

//However the cursor pops right back up to the top after popping out of 
//reload() block

Thanks in advance


Solution

  • This is a little kludgy - maybe the best strategy is just not to use older versions of ExtJS - but this seems to work okay.

    Basically just declare some global variables that can be seen from anywhere in the file - one is a flag to indicate that the event that triggers the reload has occured (rowClicked), and one to represent the last known position of the scrollbar

    var rowClicked = false;
    var prevScrollPosition = -1;
    

    When the event occurs that will trigger the re-load - set the global variable rowClicked to true

    rowclick: function(grid, rowIndex, event) {
        rowClicked = true;
    

    Then in the listeners of the grid use the rowClicked variable to determine when to re-position the scrollbar.

    listeners : {                 
        bodyscroll: function(sl, st) {
            if (rowClicked) {          
                getScenarioLineOrderNumbersGrid().getView().scroller.scrollTo('top',prevScrollPosition);
                rowClicked = false;
            }
            prevScrollPosition = st;                      
        }
    

    Kludgy - but it works.