如何使用 max-height 和 Overflow-y: 滚动自动滚动 GWT SuggestBox?

发布于 2024-12-17 15:26:07 字数 609 浏览 0 评论 0原文

如何在持有 SuggestBoxPopupPanel 上设置最大高度来自动滚动 GWT SuggestBox?目前,当用户按键盘向上键和向下键时,建议项目的样式会发生变化,并且按 Enter 键将选择列表中当前选定的项目。

当项目位于低于最大高度时,滚动条不会滚动。 我尝试扩展 SuggestBox 和内部类 DefaultSuggestionDisplay 来覆盖 moveSelectionDown()moveSelectionUp() 以显式调用 <代码>popup.setScrollTop()。

为了做到这一点,我需要访问当前选定的 MenuItem 的绝对顶部,因此需要访问 SuggestionMenu 它也是 SuggestBox 的内部类,它是私有的并声明为DefaultSuggestionDisplay 中的私有成员,不带 getter。由于 GWT 是 JavaScript,我们无法使用反射来访问它......有人有解决此问题的方法吗?

谢谢。

How can I auto scroll the GWT SuggestBox with max-height set on the PopupPanel holding the SuggestBox? Currently when the user presses keyboard up keys and down keys styles changes on the suggested items and pressing enter will select the currently selected item on the list.

When the item is located in lower than the max-height scroll bars doesn't scroll.
I tried extending the SuggestBox and inner class DefaultSuggestionDisplay to override moveSelectionDown() and moveSelectionUp() to explicitly call popup.setScrollTop().

In order to do this I need access to the absolute top of the currently selected MenuItem therefore need access to SuggestionMenu which is also an inner class of SuggestBox which is private and declared as a private member within DefaultSuggestionDisplay without getter. Since GWT is a JavaScript we can't use reflection to access it.... Does anyone have a workaround for this issue?

Thanks.

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

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

发布评论

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

评论(3

天赋异禀 2024-12-24 15:26:08

我一直在四处寻找,但找不到合适的解决方案(除了重新实现 SuggestBox 之外)。以下内容避免重新实现 SuggestBox:

private static class ScrollableDefaultSuggestionDisplay extends SuggestBox.DefaultSuggestionDisplay {

    private Widget suggestionMenu;

    @Override
    protected Widget decorateSuggestionList(Widget suggestionList) {
        suggestionMenu = suggestionList;

        return suggestionList;
    }

    @Override
    protected void moveSelectionDown() {
         super.moveSelectionDown();
         scrollSelectedItemIntoView();
    }

    @Override
    protected void moveSelectionUp() {
         super.moveSelectionUp();
         scrollSelectedItemIntoView();
    }

    private void scrollSelectedItemIntoView() {
        //                                     DIV          TABLE       TBODY       TR's
        NodeList<Node> trList = suggestionMenu.getElement().getChild(1).getChild(0).getChildNodes();
        for (int trIndex = 0; trIndex < trList.getLength(); ++trIndex) {
            Element trElement = (Element)trList.getItem(trIndex);
            if (((Element)trElement.getChild(0)).getClassName().contains("selected")) {
                trElement.scrollIntoView();

                break;
        }
    }
}

}

I've been searching around and couldn't find a proper solution (apart from reimplementing SuggestBox). The following avoids reimplementing SuggestBox:

private static class ScrollableDefaultSuggestionDisplay extends SuggestBox.DefaultSuggestionDisplay {

    private Widget suggestionMenu;

    @Override
    protected Widget decorateSuggestionList(Widget suggestionList) {
        suggestionMenu = suggestionList;

        return suggestionList;
    }

    @Override
    protected void moveSelectionDown() {
         super.moveSelectionDown();
         scrollSelectedItemIntoView();
    }

    @Override
    protected void moveSelectionUp() {
         super.moveSelectionUp();
         scrollSelectedItemIntoView();
    }

    private void scrollSelectedItemIntoView() {
        //                                     DIV          TABLE       TBODY       TR's
        NodeList<Node> trList = suggestionMenu.getElement().getChild(1).getChild(0).getChildNodes();
        for (int trIndex = 0; trIndex < trList.getLength(); ++trIndex) {
            Element trElement = (Element)trList.getItem(trIndex);
            if (((Element)trElement.getChild(0)).getClassName().contains("selected")) {
                trElement.scrollIntoView();

                break;
        }
    }
}

}

冰雪之触 2024-12-24 15:26:08

根据 Google 网上论坛的此讨论,我实现了类似的解决方案由于使用了 JSNI 而更加简洁:

private class ScrollableDefaultSuggestionDisplay extends DefaultSuggestionDisplay {

    @Override
    protected void moveSelectionDown() {
        super.moveSelectionDown();
        scrollSelectedItemIntoView();
    }

    @Override
    protected void moveSelectionUp() {
        super.moveSelectionUp();
        scrollSelectedItemIntoView();
    }

    private void scrollSelectedItemIntoView() {
        getSelectedMenuItem().getElement().scrollIntoView();
    }

    private native MenuItem getSelectedMenuItem() /*-{
        var menu = this.@com.google.gwt.user.client.ui.SuggestBox.DefaultSuggestionDisplay::suggestionMenu;
        return [email protected]::selectedItem;
    }-*/;
}

Following this discussion on Google groups, I implemented a similar solution which is a bit more concise due to the use of JSNI:

private class ScrollableDefaultSuggestionDisplay extends DefaultSuggestionDisplay {

    @Override
    protected void moveSelectionDown() {
        super.moveSelectionDown();
        scrollSelectedItemIntoView();
    }

    @Override
    protected void moveSelectionUp() {
        super.moveSelectionUp();
        scrollSelectedItemIntoView();
    }

    private void scrollSelectedItemIntoView() {
        getSelectedMenuItem().getElement().scrollIntoView();
    }

    private native MenuItem getSelectedMenuItem() /*-{
        var menu = this.@com.google.gwt.user.client.ui.SuggestBox.DefaultSuggestionDisplay::suggestionMenu;
        return [email protected]::selectedItem;
    }-*/;
}
许你一世情深 2024-12-24 15:26:08

好吧,我终于找到了解决方案。我必须基于 GWT SuggestBox 实现创建自己的建议框。但在自定义实现中请遵循以下步骤:
-将 ScrollPanel 放置到 PopupPanel,然后将 MenuBar 放置到 ScrollPanel
- 在新的内部 SuggestionDisplat 实现的 moveSelectionUp() 和 moveSelectionDown() 中添加以下代码:

 panel.ensureVisible( menu.getSelectedItem( ) );

这无法通过扩展 SuggestBox 来实现,因为我们无法访问选定的内容
MenuItem 除非重写受保护的 getSelectionItem() 方法作为公共方法。

最后添加 CSS:

max-height: 250px;

到显示实现中的 popupPanel 中。

Ok, I finally found the solution. I had to create my own suggest box based on GWT SuggestBox implementations. But follow below in custom implementaion:
-Place ScrollPanel to PopupPanel then place MenuBar to ScrollPanel
-In moveSelectionUp() and moveSelectionDown() of your new internal SuggestionDisplat implementation add the code below:

 panel.ensureVisible( menu.getSelectedItem( ) );

This is not achievable by extending the SuggestBox since we won't have access to selected
MenuItem unless overriding protected getSelectionItem() method as public method.

Finally add CSS:

max-height: 250px;

To the popupPanel in your display implementations.

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