问题
I'm looking for someone to point me in the right direction (link) or provide a code example for implementing a drop down list for a many-to-one relationship using RequestFactory
and the Editor
framework in GWT
. One of the models for my project has a many to one relationship:
@Entity
public class Book {
@ManyToOne
private Author author;
}
When I build the view to add/edit a book, I want to show a drop down list that can be used to choose which author wrote the book. How can this be done with the Editor
framework?
回答1:
For the drop-down list, you need a ValueListBox<AuthorProxy>
, and it happens to be an editor of AuthorProxy
, so all is well. But you then need to populate the list (setAcceptableValues
), so you'll likely have to make a request to your server to load the list of authors.
Beware the setAcceptableValues
automatically adds the current value (returned by getValue
, and defaults to null
) to the list (and setValue
automatically adds the value to the list of acceptable values too if needed), so make sure you pass null
as an acceptable value, or you call setValue
with a value from the list before calling setAcceptableValues
.
回答2:
I know it's an old question but here's my two cents anyway.
I had some trouble with a similar scenario. The problem is that the acceptable values (AuthorProxy
instances) were retrieved in a RequestContext
different than the one the BookEditor
used to edit a BookProxy
.
The result is that the current AuthorProxy
was always repeated in the ValueListBox
when I tried to edit a BookProxy
object. After some research I found this post in the GWT Google group, where Thomas explained that
"EntityProxy#equals() actually compares their request-context and stableId()."
So, as I could not change my editing workflow, I chose to change the way the ValueListBox
handled its values by setting a custom ProvidesKey
that used a different object field in its comparison process.
My final solution is similar to this:
@UiFactory
@Ignore
ValueListBox<AuthorProxy> createValueListBox ()
{
return new ValueListBox<AuthorProxy>(new Renderer<AuthorProxy>()
{
...
}, new ProvidesKey<AuthorProxy>()
{
@Override
public Object getKey (AuthorProxy author)
{
return (author != null && author.getId() != null) ? author.getId() : Long.MIN_VALUE;
}
});
}
This solution seems ok to me. I hope it helps someone else.
来源:https://stackoverflow.com/questions/6449356/gwt-editor-framework-drop-down-list