Results 1 to 10 of 10

Thread: Restoring vertical scroll position after Grid reloads data

  1. #1

    Default Restoring vertical scroll position after Grid reloads data

    I need to reload Grid with new data. I do not want to lose the scroll position in the Grid.

    Searching this forum, I found only a working solution for ExtJs, but not for GXT.

    The solution I came up so far (borrowing some code from GXT sources) is below. For me it looks like a hack.

    Can anybody suggest a more legal way to keep the vertical scroll position?



    //remember position
    NodeList cs = grid.getElement().getFirstChild().getChildNodes();
    El mainWrap = new El((com.google.gwt.user.client.Element) cs.getItem(0));
    El scroller = mainWrap.getChild(1);
    int top = scroller.getScrollTop();

    //reload grid
    store.removeAll();
    store.add(getNewData....());

    //restore position
    scroller.setScrollTop(top);

  2. #2

    Default How to get/set scroll position of Grid?

    I have the same need. It looks like widgets/classes that extend ScrollContainer get methods like get/setVScrollPosition() and get/setHScrollPosition(). Unfortunately Grid does not extend this class.

    A possible alternative I tried out was to place the grid in a container (like ContentPanel) that extends ScrollContainer. My hope was that the ScrollContainer would handle the scrolling for its children (ie the grid). The problem here is that I can't find a way to turn off the scrollbar in the grid, so that it's container (the ContentPanel) can handle the scrolling.

  3. #3
    Ext GWT Premium Member
    Join Date
    Sep 2008
    Posts
    48

    Default

    +1

    Grid or Grid View need to have methods that will restore the scroll position.

  4. #4

    Default

    Hi all,

    Here is a sample class which manage scrollbars on a Grid:
    [CODE]
    /**
    * This class manages the position of scrollbars in a Grid by retreiving the actual position
    * just before the loading, and restoring the memorized position after the loading.<br/>
    * This class also reset the vertical scrollbar when you are using a PagingLoadConfig
    * and when offset and/or limit has been changed.
    * <b>Usage:</b><br/>
    * <code>loader.addLoadListener(new ScrollGridManager(grid));</code>
    * @author Eric Taix
    */
    @SuppressWarnings("unchecked")
    public class ScrollGridManager extends LoadListener {
    // Last position of vertical scrollbar
    private int lastScrollTop;
    // Last position of horizontal scrollbar
    private int lastScrollLeft;
    // Last offset
    private Integer offset = null;
    // Last limit value
    private Integer limit = null;
    // La grille g

  5. #5

    Default works great

    Thanks Eric,

    Your solution works perfectly!

  6. #6
    Ext JS Premium Member
    Join Date
    Feb 2009
    Posts
    102

    Default

    Eric,

    It looks like the code you posted to this thread has been truncated. Could you please post the code again. I have the same problem and need to control the grid scrolling. Thanks.

  7. #7

    Default

    Sorry I've just seen your post tonight ! I'll post again my solution next week and also send you a PM.

  8. #8

    Default

    eric, could u repost?
    thank.

  9. #9

    Default

    Hi all,

    Here is the code again
    Code:
    /**
    * This class manages the position of scrollbars in a Grid by retreiving the actual position
    * just before the loading, and restoring the memorized position after the loading.<br/>
    * This class also reset the vertical scrollbar when you are using a PagingLoadConfig
    * and when offset and/or limit has been changed.
    * <b>Usage:</b><br/>
    * <code>loader.addLoadListener(new ScrollGridManager(grid));</code>
    * @author Eric Taix
    */
    @SuppressWarnings("unchecked")
    public class ScrollGridManager extends LoadListener {
      // The last position of the vertical scrollbar
      private int lastScrollTop;
      // The last position of the horizontal scrollbar
      private int lastScrollLeft;
      // Last offset of the paging
      private Integer offset = null;
      // Last limit of the paging
      private Integer limit = null;
      // The managed grid
      private Grid managedGrid;
      private List lastSelections;
      
      /**
       * Constructor which initialize the managed grid
       * @param gridP
       */
      public ScrollGridManager(Grid gridP) {
        managedGrid = gridP;
      }
      
      /**
       * Just before loading: memorize datas
       */
      @Override
      public void loaderBeforeLoad(LoadEvent leP) {
        boolean newPage = false;
        // If paging are used, verify if the page has changed
        if (leP.config != null && leP.config instanceof PagingLoadConfig) {
          PagingLoadConfig paging = (PagingLoadConfig)leP.config;
          if (offset == null || limit == null || offset!=paging.getOffset() || limit!=paging.getLimit()) {
            newPage = true;
          }
          offset = paging.getOffset();
          limit = paging.getLimit();
        }
        // If it is not a new page
        if (!newPage) {
          // Retrieve the current selection
          lastSelections = managedGrid.getSelectionModel().getSelection();
          // Retrieve scrollbars
          El scroller = managedGrid.getView().getBody();
          if (scroller != null) {
            lastScrollTop = scroller.getScrollTop();
            lastScrollLeft = scroller.getScrollLeft();
          }
        }
        // New page : reset the vertical scrollbar
        else {
          lastScrollLeft = 0;
          lastSelections = null;
        }
      }
    
      /**
       * After loading : set the scrollbars values
       */
      @Override
      public void loaderLoad(LoadEvent leP) {
        // Set the selection
        if (lastSelections != null && lastSelections.size() > 0) {
          managedGrid.getSelectionModel().setSelection(lastSelections);
        }
        // Set scrollbars values
        El scroller = managedGrid.getView().getBody();
        if (scroller != null) {
          scroller.setScrollLeft(lastScrollLeft);
          scroller.setScrollTop(lastScrollTop);
        }
      }
    }
    Hope it help !

  10. #10
    Ext JS Premium Member
    Join Date
    Feb 2009
    Posts
    102

    Default

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •