如何在gwt-bootstrap3中添加表单元素

时间:2015-02-23 18:21:54

标签: gwt gwtbootstrap3

我正在尝试在gwt-bootstrap3模态[link]中添加一些元素,我正在使用UI-binder来生成屏幕,但没有出现任何内容。

我的ui binder类

<?xml version="1.0" encoding="UTF-8"?>
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder' xmlns:g='urn:import:com.google.gwt.user.client.ui' xmlns:b='urn:import:org.gwtbootstrap3.client.ui'
xmlns:res="urn:with:com.db.cary.client.resources.CSSResources">
   <ui:with type="com.db.cary.client.resources.CSSResources" field="res">
   </ui:with>
   <b:Modal closable="true" fade="true" dataBackdrop="TRUE" dataKeyboard="true">
       <b:ModalBody>
           <b:Form type="HORIZONTAL">
               <b:FieldSet>
                   <b:Legend>Please enter the book detail</b:Legend>

                   <b:FormGroup>
                       <b:FormLabel for="bookTitle" addStyleNames="col-lg-2">Title</b:FormLabel>
                       <g:FlowPanel addStyleNames="col-lg-10">
                           <b:TextBox placeholder="Enter book Title" ui:field="titleTextBox" />
                       </g:FlowPanel>
                   </b:FormGroup>

                   <b:FormGroup>
                       <b:FormLabel for="bookAuthor" addStyleNames="col-lg-2">Author</b:FormLabel>
                       <g:FlowPanel addStyleNames="col-lg-10">
                           <b:ListBox ui:field="authorListBox" />
                           <b:Button ui:field="newAuthorButton" type="LINK" size="EXTRA_SMALL">New author</b:Button>
                       </g:FlowPanel>
                       <g:FlowPanel addStyleNames="col-lg-offset-2 col-lg-10">
                           <b:TextBox ui:field="authorTextBox" placeholder="enter slash (/) separated list of authors"></b:TextBox>
                       </g:FlowPanel>
                   </b:FormGroup>

                   <b:FormGroup>
                       <b:FormLabel for="bookCategory" addStyleNames="col-lg-2">Category</b:FormLabel>
                       <g:FlowPanel addStyleNames="col-lg-10">
                           <b:ListBox ui:field="categoryListBox" />
                           <b:Button ui:field="newCategoryButton" type="LINK" size="EXTRA_SMALL">New Category</b:Button>
                       </g:FlowPanel>
                       <g:FlowPanel addStyleNames="col-lg-offset-2 col-lg-10">
                           <b:TextBox ui:field="categoryTextBox" placeholder="enter category"></b:TextBox>
                       </g:FlowPanel>
                   </b:FormGroup>
               </b:FieldSet>
           </b:Form>
       </b:ModalBody>
       <b:ModalFooter>
           <b:Button type="PRIMARY" ui:field='submitButton'>Submit</b:Button>
           <b:Button ui:field='cancelButton'>Cancel</b:Button>
       </b:ModalFooter>
   </b:Modal>
</ui:UiBinder>

和我的观点类

public class AddBook extends Modal {

   interface CheckOutPopUpBinder extends UiBinder<Widget, AddBook> {
   }

   private static final CheckOutPopUpBinder binder = GWT.create(CheckOutPopUpBinder.class);
   private final AuthorAndCategoryServiceAsync authorService = GWT.create(AuthorAndCategoryService.class);
   private final LibraryServiceAsync libraryServiceAsync = GWT.create(LibraryService.class);

   @UiField
   TextBox titleTextBox;
   @UiField
   ListBox authorListBox;
   @UiField
   TextBox authorTextBox;
   @UiField
   ListBox categoryListBox;
   @UiField
   Button submitButton;
   @UiField
   Button cancelButton;

   @UiField
   Button newAuthorButton;
   @UiField
   Button newCategoryButton;
   @UiField
   TextBox categoryTextBox;

   public AddBook(String title) {
      binder.createAndBindUi(this);
      setTitle(title);
      initializeAuthorListBox();
      initializeCategoryListBox();
   }

   private void initializeCategoryListBox() {
      authorService.getCategories(null, new AsyncCallback<List<CategoryDTO>>() {

     @Override
     public void onFailure(Throwable arg0) {
        Window.alert("unable to fetch category list");
     }

     @Override
     public void onSuccess(List<CategoryDTO> arg0) {
        for (CategoryDTO category : arg0)
           categoryListBox.addItem(category.getCategoryName());
     }
      });
      categoryListBox.setMultipleSelect(false);
      categoryTextBox.setVisible(false);

   }

   private void initializeAuthorListBox() {
      authorService.getAuthors(null, new AsyncCallback<List<AuthorDTO>>() {
     @Override
     public void onSuccess(List<AuthorDTO> arg0) {
        for (AuthorDTO author : arg0) {
           authorListBox.addItem(author.getAuthorName());
        }
     }

     @Override
     public void onFailure(Throwable arg0) {
        Window.alert("Unable to fetch the list of authors");
     }
      });
      authorListBox.setMultipleSelect(true);
      authorTextBox.setVisible(false);
   }

   @UiHandler("cancelButton")
   public void cancelAction(ClickEvent e) {
      AddBook.this.hide();
   }

   @UiHandler("submitButton")
   public void submitAction(ClickEvent e) {
      AddBookDTO bookDTO = new AddBookDTO();
      String bookTitle = titleTextBox.getText();
      String bookCategory = categoryListBox.getSelectedValue() == null ? categoryTextBox.getText() : categoryListBox.getSelectedValue();
      List<String> authorsList = new ArrayList<String>();

      for (int i = 0; i < authorListBox.getItemCount(); i++) {
     if (authorListBox.isItemSelected(i)) {
        authorsList.add(authorListBox.getItemText(i));
     }
      }

      if (null != authorTextBox.getText() && authorTextBox.getText().trim().length() > 0) {
     String[] values = authorTextBox.getText().split("/");
     for (String str : values) {
        authorsList.add(str);
     }
      }
      if (bookTitle == null || bookTitle.length() <= 0) {
     Window.alert("Please enter a valid book title");
     return;
      } else if (bookCategory == null || bookCategory.length() <= 0) {
     Window.alert("Please enter a valid book category");
     return;
      } else if (authorsList == null || authorsList.size() == 0) {
     Window.alert("Please enter valid authors");
     return;
      }
      bookDTO.setBookTitle(bookTitle);
      bookDTO.setCategroyName(bookCategory);
      bookDTO.setAuthors(authorsList);
      libraryServiceAsync.addBook(bookDTO, new AsyncCallback<Boolean>() {

     @Override
     public void onFailure(Throwable arg0) {
        Window.alert("There is some issue with database while adding book, Please contact your admin");

     }

     @Override
     public void onSuccess(Boolean arg0) {
        Window.alert("Book is successfully added !!!");
     }
      });
      this.hide();

   }

   @UiHandler("newAuthorButton")
   public void addAuthor(ClickEvent e) {
      authorTextBox.setVisible(true);
   }

   @UiHandler("newCategoryButton")
   public void addCategory(ClickEvent e) {
      categoryTextBox.setVisible(true);
   }

}

我不确定,有什么问题,但只有标题出现在模态中。

1 个答案:

答案 0 :(得分:1)

您正在调用AddBook.this.show(); - 这会显示Modal实例的基础AddBook,而不是您在UiBinder模板中定义的实例。当您调用setTitle(title);时,您正在此实例上设置标题/标题 - 这就是为什么您看到的只是标题而不是模式的其余部分。您应该为您的UiBinder模板中定义的ui:field分配Modal并显示/隐藏它。

另外AddBook不应该扩展Modal - 它根本不应该扩展任何窗口小部件类:)通常,UiBinder类正在扩展Composite - 因为你的UiBinder模板由各种小部件组成,Composite用于将它们组合在一起而不暴露任何API:您使用initWidget的结果调用binder.createAndBindUi(this)。 但是如果你正在创建一个小部件,那就是&#34; main&#34;小部件是Modal,就像这里一样,您应该只调用binder.createAndBindUi(this)并忽略返回的Widget(就像您现在正在做的那样)。这是因为Modal将自己附加到DOM,绕过任何GWT机制(实际上,它与它发生冲突)。