表单验证和SuggestBox选择之间的GWT冲突

时间:2017-05-26 12:33:06

标签: java gwt widget

我的GWT SuggestBox小部件存在问题。

我在表单的每个字段都有一个处理程序,用enter键提交它。 不过我也想用enter键填充建议框。

因此,我正试图找到一个合适的方法: 1.当我在建议框中选择一个项目(显示建议菜单)并按回车键时,选择此项目 2.当建议菜单未显示验证表格时

任何想法

1 个答案:

答案 0 :(得分:1)

当您查看SuggestBox构造函数时,您会发现this

public SuggestBox(SuggestOracle oracle,
                  ValueBoxBase<java.lang.String> box,
                  SuggestBox.SuggestionDisplay suggestDisplay)
  

oracle - 根据文本小部件的当前内容提供建议

     

框 - 文本小部件

     

suggestDisplay - 用于显示建议的类

SuggestionDisplay是您需要检查isSuggestionListShowing()

示例代码:

@UiField
FormPanel uiFormPanel;
@UiField
TextBox uiTextBox;
@UiField(provided = true)
SuggestBox uiSuggestBox;

public SubmitTest() {

    // provided fields
    MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
    oracle.add("Cat");
    oracle.add("Dog");
    oracle.add("Horse");
    oracle.add("Canary");

    TextBox box = new TextBox();
    final DefaultSuggestionDisplay suggestDisplay = new DefaultSuggestionDisplay();

    uiSuggestBox = new SuggestBox(oracle, box, suggestDisplay);

    initWidget(uiBinder.createAndBindUi(this));

    uiFormPanel.addSubmitHandler(new SubmitHandler() {
        @Override
        public void onSubmit(SubmitEvent event) {
            event.cancel();
            Window.alert("Submit!");
        }
    });

    uiTextBox.addKeyDownHandler(new KeyDownHandler() {
        @Override
        public void onKeyDown(KeyDownEvent event) {
            if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
                uiFormPanel.submit();
        }
    });

    uiSuggestBox.addKeyDownHandler(new KeyDownHandler() {
        @Override
        public void onKeyDown(KeyDownEvent event) {
            if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
                if(!suggestDisplay.isSuggestionListShowing())
                    uiFormPanel.submit();
        }
    });
}

UiBinder的:

<g:FormPanel ui:field="uiFormPanel">
    <g:HTMLPanel>
        <g:TextBox ui:field="uiTextBox" />
        <g:SuggestBox ui:field="uiSuggestBox" />
    </g:HTMLPanel>
</g:FormPanel>