Patrick
Patrick

Reputation: 1040

Showing an initially selected object in an ObjectAutoCompleteField on page load in Wicket

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.

Upvotes: 2

Views: 648

Answers (1)

Patrick
Patrick

Reputation: 1040

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.

Upvotes: 1

Related Questions