在 Wicket 中加载页面时在 ObjectAutoCompleteField 中显示最初选择的对象

发布于 2024-10-05 09:56:46 字数 419 浏览 5 评论 0原文

我已按照 Wicket 示例指南 来让 ObjectAutoCompleteField 正常工作,并且它确实非常好。

不过,我有一个很大的问题,那就是在页面加载时在字段中显示初始设置的对象。该对象是从我用于使用 ObjectAutoCompleteField 的表单的模型中检索的。更改 ObjectAutoCompleteField 会更改它“连接”到的模型属性,并且该字段中的任何后续更改都会在其位置显示适当的标签,而不是页面加载时的初始标签 - 唯一显示的是编辑链接(到进入自动完成功能)。

我查看了 ObjectAutoCompleteBuilder 的文档,但没有找到任何相应的方法来在页面加载时显式设置初始值。

I've followed the Wicket by Example guide to get the ObjectAutoCompleteField working, and it does so quite nicely.

I have a huge problem, though, and that is to show an initially set object in the field when the page loads. The object is retrieved from a model I use for the form where the ObjectAutoCompleteField is used. Changing the ObjectAutoCompleteField changes the model attribute it is "connected" to, and any subsequent changes in the field shows the appropriate label in its place, just not the initial one when the page loads—the only thing that shows is the edit link (to get to the autocomplete functionality).

I've looked around in the documentation for the ObjectAutoCompleteBuilder but haven't found any corresponding method to even set the initial value explicitly on page load.

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

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

发布评论

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

评论(1

烦人精 2024-10-12 09:56:46

我终于通过查看与ObjectAutoCompleteField相关的类找到了解决方案。

ObjectAutoCompleteField是通过ObjectAutoCompleteBuilder中的build方法构造的。因此,通过调用构建器上的 readOnlyRenderer 方法,创建一个新的 ObjectReadOnlyRenderer 在其 getObjectRenderer 内创建一个标签,我得到了 ObjectAutoCompleteField 在页面加载时渲染预选的对象。

ObjectAutoCompleteBuilder<Author, Long> builder = new ObjectAutoCompleteBuilder<Author, Long>(provider);
builder.readOnlyRenderer(new ObjectReadOnlyRenderer<Long>() {
    public Component getObjectRenderer(String id, IModel<Long> pModel, IModel<String> pSearchTextModel) {
        return new Label(id, new PropertyModel<Author>(model, "author"));
    }
});

人们可能会认为这是标准行为,但现在我知道以供将来参考。

I finally managed to find a solution by looking through the classes relating to ObjectAutoCompleteField.

The ObjectAutoCompleteField is constructed by the build method in ObjectAutoCompleteBuilder. So, by calling the readOnlyRenderer method on the builder, creating a new ObjectReadOnlyRenderer creating a label inside its getObjectRenderer, I got the ObjectAutoCompleteField to render a preselected object on page load.

ObjectAutoCompleteBuilder<Author, Long> builder = new ObjectAutoCompleteBuilder<Author, Long>(provider);
builder.readOnlyRenderer(new ObjectReadOnlyRenderer<Long>() {
    public Component getObjectRenderer(String id, IModel<Long> pModel, IModel<String> pSearchTextModel) {
        return new Label(id, new PropertyModel<Author>(model, "author"));
    }
});

One would think that this was the standard behaviour, but now I know for future reference.

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