GWT- Suggestbox listener not working

不羁的心 提交于 2019-12-11 17:54:11

问题


I need to add a handler that fires when a selection is CLICKED which will then validate the value. Current functionality is validating (through textInput on blur) right before the entire value is recorded from the suggestbox, thus not passing validation (when it should).

Here is what i tried right below where i implement the suggestbox in the TextInput page:

public void onModuleLoad() {

SuggestBox box = new SuggestBox(createListOracle(),myTextBox());

box.addSelectionHandler(new SelectionHandler<Suggestion>() {

    @Override
    public void onSelection(SelectionEvent<Suggestion> event) {
        Validate();
    }
});

another solution could be to insert the courser on focus when suggestbox is selected from, that would accomplish the same thing for me.

The problem is the handler is never firing. The break-point is never reached.


回答1:


Then take a look at ValueBoxBase.

You will pass your own instance to the constructor of the SuggestBox

public SuggestBox(SuggestOracle oracle, ValueBoxBase box)

TextBox is a subclass of ValueBoxBase, and it has ClickListeners so you have the choice between:

  1. Create your TextBox outside and add it listeners, then pass it to the constructor SuggestBox(SuggestOracle oracle, ValueBoxBase box)
  2. Overriding SuggestBox and making the constructor take a "better" ValueBoxBase (for example TextBox) and add the listener methods to your implmentation

I tried this sample, it works

        TextBox suggestTextBox = new TextBox();
    suggestTextBox.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            Window.alert("tada");
        }
    });
    SuggestOracle oracle = new MultiWordSuggestOracle(" ,");

    final SuggestBox nameField = new SuggestBox(oracle, suggestTextBox);



回答2:


You could also use the advanced suggest box https://code.google.com/p/advanced-suggest-select-box/

which gives you the control over the events: you can override valueSelected() or valueTyped() and decide whether to validate it or not.

The demo of the lib is here http://1vu-widgets.appspot.com/IntoGwt.html



来源:https://stackoverflow.com/questions/19599046/gwt-suggestbox-listener-not-working

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