How to hide suggestions in GWT SuggestBox?

亡梦爱人 提交于 2019-12-06 16:39:36

What you can do is simply swap the SuggestionBox with a normal TextBox when the user clicks edit and back when edit is closed. Also because if you would hide the suggestions list, it still queried from the server. By swapping the widget you don't have to care about side effects. SuggestionBox itself uses also a TextBox and thus for the user it's not visible the widget has changed.

If you don't use your own SuggestionDisplay, then this should Just Work™:

((DefaultSuggestionDisplay) suggestBox.getSuggestionDisplay()).hideSuggestions();
Vijay Sarin

Here is the Solution

My Entry Point Class

public class SuggestionEntryPoint implements EntryPoint {

    @Override
    public void onModuleLoad() {
        SuggestBoxWidget suggestBoxWidget = new SuggestBoxWidget();
        RootPanel rootPanel = RootPanel.get();
        suggestBoxWidget.createOracle();
        suggestBoxWidget.createWidgetAndShow(rootPanel);
        rootPanel.add(suggestBoxWidget);
        DOM.getElementById("loader").removeFromParent();
    }

}

And here is my Widget

public class SuggestBoxWidget extends Composite {

    private TextBox textSuggestBox = new TextBox();

    private SuggestBox suggestBox = null;

    DefaultSuggestionDisplay suggestionDisplay = new DefaultSuggestionDisplay();

    MultiWordSuggestOracle suggestOracle = new MultiWordSuggestOracle();

    private static SuggestBoxWidgetUiBinder uiBinder = GWT
        .create(SuggestBoxWidgetUiBinder.class);

    interface SuggestBoxWidgetUiBinder extends
        UiBinder<Widget, SuggestBoxWidget> {
    }

    public SuggestBoxWidget() {
        initWidget(uiBinder.createAndBindUi(this));     
    }

    public void registerEvents(){
        suggestBox.addKeyUpHandler(new KeyUpHandler() {
            @Override
            public void onKeyUp(KeyUpEvent event) {
                if(suggestBox.getText().equalsIgnoreCase("1")){                 
                    suggestionDisplay.hideSuggestions();
                }
            }
        });
    }

    public void createWidgetAndShow(HasWidgets container){
        suggestBox = new SuggestBox(suggestOracle,textSuggestBox,suggestionDisplay);
        container.clear();
        container.add(suggestBox);      
        registerEvents();
    }

    public void createOracle(){
        for(int i=1;i<=100;i++){
            suggestOracle.add(i+"");
        }
    }

}

Actually you have to create a SuggestBox with 3 Parameters to the Constructor.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!