简体   繁体   English

使用GWT编辑器框架创建包含CellList的模型?

[英]Create model with containing CellList using GWT Editor Framework?

I have the following model: 我有以下模型:

class Book {
  String title;
  // getter and setter
}

class Author {
  private name;
  private List<Book> books;
  // getter and setter  
}

I want to create a GWT Widget where I can create an author and create as many books as possible while creating the author in the same view. 我想创建一个GWT窗口小部件,可以在同一视图中创建作者的同时创建作者并创建尽可能多的书籍。 Here is how it might look like: 这可能是这样的:

在此处输入图片说明

You can add as many books as you want: 您可以根据需要添加任意数量的图书:

在此处输入图片说明

I want to use the GWT Editor framework to populate the data between the view and the models. 我想使用GWT编辑器框架在视图和模型之间填充数据。 Here is how I do it only for the Author model without the list. 这是我仅对没有列表的Author模型执行的操作。

My UiBinder: 我的UiBinder:

<g:TextBox ui:field="nameInput"/>

My Widget.java: 我的Widget.java:

public class MyWidgetView {

  @Path(value="name")
  @UiField
  TextBox nameInput;

public interface EditorDriver extends SimpleBeanEditorDriver<Author, MyWidgetView> { }

  @Override
  public SimpleBeanEditorDriver<Author, ?> createEditorDriver() {
    EditorDriver driver = GWT.create(EditorDriver.class);
    driver.initialize(this);
    return driver;
  }

}

In my presenter I created: 在我的演示者中,我创建了:

SimpleBeanEditorDriver<TriggerDto, ?> editorDriver = getView().createEditorDriver();

This works fine for a simple model but I want to use it for the above author model which contains a List<Book> . 这对于简单的模型来说效果很好,但是我想在上面包含List<Book>作者模型中使用它。

How do I have to setup the UiBinder such that I can add Books to the author and what do I have to do with the editor to get the created list of Book s flushed into the author model? 如何设置UiBinder,以便可以将Book添加到作者中,并且与编辑器有什么关系才能将创建的Book列表刷新到作者模型中?

Here how you can achieve that. 在这里,您将如何实现这一目标。 You'll need to create an Editor, a ListEditor and an EditorSource 您需要创建一个编辑器,一个ListEditor和一个EditorSource

public class BooksEditor implements IsWidget, IsEditor<ListEditor<Book, BookEditor>> {
    private final HTMLPanel main;
    private final ListEditor<Book, BookEditor>> editor;

    @Inject
    BooksEditor(
            Provider<BookEditor> bookEditorProvider) {
        main = new HTMLPanel("");
        editor = ListEditor.of(new BookEditorSource(bookEditorProvider, main));
    }

    @Override
    public ListEditor<Book, BookEditor> asEditor() {
        return editor;
    }

    @Override
    public Widget asWidget() {
        return main;
    }`
}

public class BookEditorSource extends EditorSource<BookEditor> {
    private final Provider<BookEditor> bookEditorProvider;
    private final HTMLPanel books;

    public BookEditorSource(
            Provider<BookEditor> bookEditorProvider,
            HTMLPanel books) {
        this.bookEditorProvider = bookEditorProvider;
        this.books = books;
    }

    @Override
    public BookEditor create(int index) {
        BookEditor bookEditor = bookEditorProvider.get();
        books.add(bookEditor);

        return bookEditor;
    }

    @Override
    public void dispose(BookEditor subEditor) {
        super.dispose(subEditor);

        books.remove(subEditor);
    }
}

To create the BookEditor class, simply create a new Widget that implements Editor<Book> and create the according UiFields to be able to edit your Book object. 要创建BookEditor类,只需创建一个新的Widget,该Widget实现Editor<Book>并创建相应的UiFields即可编辑Book对象。 The values will be handled by the parent Author SimpleBeanDriver. 这些值将由父Author SimpleBeanDriver处理。 Then in your MyWidgetView , simply add a UiField : 然后在您的MyWidgetView ,只需添加一个UiField:

@UiField(provided = true)
@Path("books")
BooksEditor booksInput;

If you want to add/remove widgets on the fly and synchronize it with the ListEditor, simply work with the BooksEditor class. 如果要动态添加/删除小部件并将其与ListEditor同步,只需使用BooksEditor类即可。 editor.getList().add(new Book()) will automatically add a new BookEditor in your panel. editor.getList().add(new Book())将在您的面板中自动添加一个新的BookEditor。 Calling editor.getList().remove(bookToRemove) will remove it (see the dispose() method in BookEditorSource, it's called when calling remove on the list). 调用editor.getList().remove(bookToRemove)将其删除(请参阅BookEditorSource中的dispose()方法,在列表上调用remove时会调用它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GWT CellList渲染完成处理程序? - GWT CellList Render Done Handler? ExtJs-如何使用包含模型配置的json文件动态创建Ext.data.Model实例 - ExtJs - How to create instance of Ext.data.Model dynamically using json file containing Model configs 有没有一种方法可以使用JavaScript创建GWT窗口小部件? - Is there a way to create GWT Widgets using JavaScript? 为何选择GWT?使用此RIA框架的优势和权衡 - Why GWT? Advantages and Trade-Offs of Using This RIA Framework 使用ajax创建在线实时文本编辑器 - Create online realtime text editor using ajax 使用jQuery创建一个包含目标div的div - Create a div containing target div using jQuery 使用GWT将doubles的JS数组(可能包含空条目)集成到Java对象的适当方法 - Appropriate method to integrate a JS array of doubles (possibly containing null-entries) to a Java object using GWT 创建模型对象以在MVC框架(如Backbone)中提交表单 - Create Model object for form submission in MVC framework like Backbone Sharepoint 框架:如何创建一个<img>包含 MSGraph 照片/$value 响应 BLOB 的标签 - Sharepoint Framework: How to create an <img> tag containing MSGraph photo/$value response BLOB 在ASP.NET MVC 3上使用iframe创建编辑器 - Using an iframe on asp.net mvc 3 to create an editor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM