I can generate a list of strings used to select an item using AutoCompleteTextField but it puts the entire string in the edit control. I would like it to just insert the 'name' string.
Should I create a Model that contains the name string and the rendered string?
Which functions should I override to get the required string, to get a value or to handle the click?
private Model<String> groupToJoinModel = new Model<String>();
final AutoCompleteTextField<String> field = new AutoCompleteTextField<String>("ac", new Model<String>(""))
{
private static final long serialVersionUID = 1L;
@Override
protected Iterator<String> getChoices(String input)
{
List<String> choices = new ArrayList<String>(5);
// from a database: generate lookup items
// by concatenating strings: name, type, description
// code omitted
return choices.iterator();
}
};
form.add(field);
groupToJoinModel = (Model<String>) field.getDefaultModel();
// Create a button to perform an action
Button joinGroupsButton = new Button("joinGroupButton")
{
private static final long serialVersionUID = -4974389888115885756L;
@Override
public void onSubmit()
{
if (groupToJoinModel.getObject() != null)
{
// An action is performed on the contents of the edit control
}
}
};
form.add(joinGroupsButton);
You can use AbstarctAutoCompleteRenderer.
AbstractAutoCompleteRenderer<String> autoCompleteRenderer = new AbstractAutoCompleteRenderer<String>() {
private static final long serialVersionUID = 1L;
protected final String getTextValue(final String bean) {
String name;
// Do you logic to extract the name from the bean
...
...
...
return name;
}
@Override
protected final void renderChoice(final String object, final Response response, final String criteria) {
response.write(getTextValue(object));
}
};
final AutoCompleteTextField<String> autoComp = new AutoCompleteTextField<String>("item", new PropertyModel(str, "item"),
autoCompleteRenderer) {
private static final long serialVersionUID = 1L;
@Override
protected Iterator<String> getChoices(String arg0) {
// Your logic
...
...
...
return filteredList.iterator();
}
};
The renderer is passed in the Auto complete constructor.