1

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

我的 ui 活页夹类

<?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);
   }

}

我不确定,出了什么问题,但模式中只出现了标题。

4

1 回答 1

1

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

也不AddBook应该扩展Modal- 它根本不应该扩展任何小部件类:) 通常,UiBinder 类正在扩展Composite- 因为您的 UiBinder 模板由各种小部件组成,并且Composite用于将它们组合在一起而不暴露它们的任何 API : 你打电话initWidget的结果是binder.createAndBindUi(this). 但是,如果您正在创建一个“主要”小部件为 的小部件Modal,就像这里一样,您应该调用binder.createAndBindUi(this)并忽略Widget返回的那个(就像您现在所做的那样)。这是因为Modal将自身附加到 DOM,绕过任何 GWT 机制(实际上,它与它冲突)。

于 2015-02-26T13:54:19.530 回答