Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT 2.4 DataGrid automatic scrolling when selecting an item

I am using GWT 2.4's new DataGrid in a project. I configured the DataGrid with a pagesize of 50.
The available screen is not big enough to display all items and thus a vertical scrollbar is shown (this is actually the main purpose for using a DataGrid in the first place).
I attached a SingleSelectionModel to the DataGrid in order to be able to select items.
This works fine so far.

However I also have another widget with which the user can interact. Based on that user action a item from the DataGrid should be selected.
Sometimes the selected item is not in the visible screen region and the user has to scroll down in the DataGrid to see it.
Is there any way to automatically or manually scroll down, so that the selected item is visible?
I checked the JavaDocs of the DataGrid and found no appropriate method or function for doing that.

like image 908
Ümit Avatar asked Sep 28 '11 11:09

Ümit


5 Answers

Don't know if this works, but you could try to get the row element for the selection and use the scrollIntoView Method.

Example Code:

dataGrid.getRowElement(INDEX_OF_SELECTED_ITEM).scrollIntoView();
like image 198
ocaner Avatar answered Mar 23 '23 10:03

ocaner


The answer above works pretty well, though if the grid is wider than your window and has a horizontal scroll bar, it also scrolls all the way to the right which is pretty annoying. I was able to get it to scroll down and stay scrolled left by getting the first cell in the selected row and then having it scroll that into view.

dataGrid.getRowElement(dataGrid.getVisibleItems().indexOf(object)).getCells().getItem(0).scrollIntoView();
like image 36
Kern Avatar answered Mar 23 '23 10:03

Kern


Don't have time to try it out, but DataGrid implements the interface HasRows, and HasRows has, among other things, a method called setVisibleRange. You just need to figure out the row number of the item that you want to focus on, and then set the visible range from that number n to n+50. That way the DataGrid will reset to put that item at the top (or near the top if it is in the last 50 elements of the list backing the DataGrid). Don't forget to redraw your DataGrid.

Have you already looked at this? If so, I'd be surprised that it didn't work.

Oh, and since this is one widget talking to another, you probably have some messaging set up and some message handlers so that when the user interacts with that second widget and "selects" the item, the message fires on the EventBus and a handler for that message fixes up the DataGrid along the lines I've described. I think you'll have to do this wiring yourself.

like image 29
Steve J Avatar answered Mar 23 '23 08:03

Steve J


My solution, a little better:

dataGrid.getRow(model).scrollIntoView();
like image 45
jordiburgos Avatar answered Mar 23 '23 10:03

jordiburgos


I got a Out of bounds exception doing the above.

I solved it getting the ScrollPanel in the DataGrid and used .scrollToTop() and so on on the ScrollPanel. However, to access the ScrollPanel in the DataGrid I had to use this comment: http://code.google.com/p/google-web-toolkit/issues/detail?id=6865

like image 40
AndreasS Avatar answered Mar 23 '23 08:03

AndreasS