GWT 编辑器框架:下拉列表

发布于 2024-11-17 00:54:43 字数 323 浏览 2 评论 0原文

我正在寻找有人为我指明正确的方向(链接)或提供代码示例,以使用 RequestFactoryEditor 实现多对一关系的下拉列表GWT 中的 框架。我的项目的一个模型具有多对一的关系:

@Entity
public class Book {

  @ManyToOne
  private Author author;
}

当我构建视图来添加/编辑一本书时,我想显示一个下拉列表,可用于选择哪位作者写了这本书。如何使用 Editor 框架来完成此操作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉殇 2024-11-24 00:54:43

对于下拉列表,您需要一个 ValueListBox,而它恰好是 AuthorProxy 的编辑器,所以一切都很好。但随后您需要填充列表 (setAcceptableValues),因此您可能必须向服务器发出请求来加载作者列表。

请注意,setAcceptableValues 会自动将当前值(由 getValue 返回,默认为 null)添加到列表(以及 setValue)如果需要的话,code> 也会自动将该值添加到可接受值列表中),因此请确保将 null 作为可接受的值传递,或者使用以下值调用 setValue调用之前的列表 setAcceptableValues

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.

萌面超妹 2024-11-24 00:54:43

我知道这是一个老问题,但无论如何,这是我的两分钱。

我在类似的情况下遇到了一些麻烦。问题在于,在 RequestContext 中检索到的可接受值(AuthorProxy 实例)与 BookEditor 用于编辑 的值不同BookProxy

结果是,当我尝试编辑 BookProxy 对象时,当前的 AuthorProxy 总是在 ValueListBox 中重复。经过一番研究,我在 GWT Google 中发现了这篇文章小组,托马斯解释说

“EntityProxy#equals() 实际上比较它们的请求上下文和 stableId()。”

因此,由于我无法更改编辑工作流程,因此我选择通过设置在比较中使用不同对象字段的自定义 ProvidesKey 来更改 ValueListBox 处理其值的方式过程。

我的最终解决方案与此类似:

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

这个解决方案对我来说似乎没问题。我希望它对其他人有帮助。

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 ValueListBoxwhen 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文