Search code examples
gwtgwtbootstrap3

Submit form without refreshing page using gwt and gwtbootstrap3


I am using gwt and gwtbootstrap3 for my gwt project. My code looks like

  <b:NavbarForm pull="LEFT">
      <b:TextBox placeholder="Search" addStyleNames="col-lg-8"/>
    </b:NavbarForm>

Now if I type anything in the textbox and hit enter, gwt is reloading whole page, which I do not wants, All I wants is to get the value from the text box and call a method. Is there any way I can achieve that.

FYI, I can fetch the value if I do not use NavbarForm but then I can not make search box inline.

http://gwtbootstrap3.github.io/gwtbootstrap3-demo/#navbar

FYI, just an update if I wrap the text box in some other container like NavbarText or NavbarNav the UI got corrupted.


Solution

  • It might be a bug in gwtbootstrap3 - they did an overhaul of the Form widget recently.

    But for now, you can add a SubmitHandler to your NavbarForm and cancel the SubmitEvent to prevent it from reloading the page:

    navbarForm.addSubmitHandler(new SubmitHandler() {
        @Override
        public void onSubmit(SubmitEvent event) {
            event.cancel();
        }
    });
    

    When using UiBinder:

    @UiHandler("navbarForm")
    void onNavbarSubmit(SubmitEvent event) {
        event.cancel();
    }
    

    Note that in this case SubmitEvent is of type org.gwtbootstrap3.client.ui.base.form.AbstractForm.SubmitEvent, not com.google.gwt.user.client.ui.FormPanel.SubmitEvent.