GWT 2.4 DataGrid选择项目时自动滚动

发布于 2024-12-06 19:40:10 字数 563 浏览 0 评论 0原文

我在项目中使用 GWT 2.4 的新 DataGrid 。我将 DataGrid 配置为页面大小 50。
可用屏幕不够大,无法显示所有项目,因此会显示垂直滚动条(这实际上是首先使用 DataGrid 的主要目的)。
我将 SingleSelectionModel 附加到 DataGrid 以便能够选择项目。
到目前为止效果很好。

不过,我还有另一个用户可以与之交互的小部件。根据该用户操作,应选择 DataGrid 中的一个项目。
有时,所选项目不在可见屏幕区域中,用户必须在 DataGrid 中向下滚动才能看到它。
有没有办法自动或手动向下滚动,以便所选项目可见?
我检查了 DataGrid 的 JavaDocs 并没有找到合适的方法或函数来执行此操作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怎言笑 2024-12-13 19:40:11

不知道这是否有效,但您可以尝试获取选择的行元素并使用scrollIntoView方法。

示例代码:

dataGrid.getRowElement(INDEX_OF_SELECTED_ITEM).scrollIntoView();

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();
傾城如夢未必闌珊 2024-12-13 19:40:11

上面的答案效果很好,但如果网格比窗口宽并且有水平滚动条,它也会一直滚动到右侧,这非常烦人。我能够通过获取所选行中的第一个单元格然后将其滚动到视图中来使其向下滚动并保持向左滚动。

dataGrid.getRowElement(dataGrid.getVisibleItems().indexOf(object)).getCells().getItem(0).scrollIntoView();

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();
傲世九天 2024-12-13 19:40:11

没有时间尝试,但是 DataGrid 实现了 HasRows 接口,并且 HasRows 除其他外还有一个名为 setVisibleRange 的方法。您只需计算出要关注的项目的行号,然后将可见范围设置为从该数字 n 到 n+50。这样,DataGrid 将重置以将该项目放在顶部(如果它位于支持 DataGrid 的列表的最后 50 个元素中,则放在顶部附近)。不要忘记重新绘制 DataGrid。

你已经看过这个了吗?如果是这样,我会很惊讶它不起作用。

哦,由于这是一个与另一个部件通信的小部件,您可能设置了一些消息传递和一些消息处理程序,以便当用户与第二个小部件交互并“选择”该项目时,消息会在 EventBus 上触发,并且会触发一个处理程序该消息按照我所描述的方式修复了 DataGrid。我想你必须自己做这个接线。

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.

早茶月光 2024-12-13 19:40:11

我的解决方案,更好一点:

dataGrid.getRow(model).scrollIntoView();

My solution, a little better:

dataGrid.getRow(model).scrollIntoView();
我家小可爱 2024-12-13 19:40:11

我执行上述操作时遇到了越界异常。

我解决了在 DataGrid 中获取 ScrollPanel 并在 ScrollPanel 上使用 .scrollToTop() 等问题。但是,要访问 DataGrid 中的 ScrollPanel,我必须使用以下注释:
http://code.google.com/p/google -web-toolkit/issues/detail?id=6865

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

简单气质女生网名 2024-12-13 19:40:11

正如 Kem 指出的,scrollIntoView 之后的“scrollToRight”效果很烦人。在我之后,Kem 的解决方案提供了比基本解决方案更好的行为,因为通常表中的第一列更有意义。
我改进了他的方法,即水平滚动到我们想要可见的行的第一列,方法是在应用滚动之前计算左侧的第一个可见列,然后滚动到它。

最后一点:绝对左侧的列是针对“51”进行测试的。这是我通过在浏览器的开发工具中查看 JS 值“实验”找到的值,我认为这取决于表格的样式,您可能需要更改/计算它。

代码如下:

public void scrollIntoView(T next) {
        int index = datagrid.getVisibleItems().indexOf(next);
        NodeList<TableCellElement> cells = datagrid.getRowElement(index).getCells();
        int firstVisibleIndex = -1;
        for(int i=0; i<cells.getLength() && firstVisibleIndex<0;i++)
            if(UIObject.isVisible(cells.getItem(i)) && (cells.getItem(i).getAbsoluteLeft() > 51) && (cells.getItem(i).getAbsoluteTop() > 0))
                firstVisibleIndex = i;

        cells.getItem(firstVisibleIndex>=0? firstVisibleIndex : 0).scrollIntoView();
}

As Kem pointed out, it's annoying the "scrollToRight" effect after the scrollIntoView. After me, Kem's solution gives a better behaviour than the base one as usually the first columns in a table are the more meaningful.
I improved a bit his approach, which scrolls horizontally to the first column of the row we want to be visible, by calculating the first visible column on the left before applying the scroll and then scrolling to it.

A final note: Columns absolute left is tested against "51". This is a value I found "experimentally" by looking the JS values in the browser's developer tool, I think it depends on the table's style, you may need to change/calculate it.

Below the code:

public void scrollIntoView(T next) {
        int index = datagrid.getVisibleItems().indexOf(next);
        NodeList<TableCellElement> cells = datagrid.getRowElement(index).getCells();
        int firstVisibleIndex = -1;
        for(int i=0; i<cells.getLength() && firstVisibleIndex<0;i++)
            if(UIObject.isVisible(cells.getItem(i)) && (cells.getItem(i).getAbsoluteLeft() > 51) && (cells.getItem(i).getAbsoluteTop() > 0))
                firstVisibleIndex = i;

        cells.getItem(firstVisibleIndex>=0? firstVisibleIndex : 0).scrollIntoView();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文