Hi Martin,
* your right, onSelected is a method of AutoCompleteTextField
* it doesn´t matter whether your write
final Form<Country> form = new Form<Country>("form")
or
final Form<String> form = new Form<String>("form")
or
final Form form = new Form("form")
or
final Form<IModel<Country>> form = new
Form<IModel<Country>>("form")
- Markus
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
JAVA:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.wicket.IClusterable;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import
com.googlecode.wicket.jquery.ui.form.autocomplete.AutoCompleteTextField;
import com.googlecode.wicket.jquery.ui.panel.JQueryFeedbackPanel;
import com.googlecode.wicket.jquery.ui.renderer.TextRenderer;
public class TestSebastienPage
{
private static final long serialVersionUID = 1L;
public TestSebastienPage()
{
// Model //
final IModel<Country> model = new Model<Country>();
// Form //
final Form form = new Form("form")
{
@Override
protected void onSubmit()
{
super.onSubmit();
System.out.println(model);
info(String.format("Selected country: %s (+%d)",
model.getObject().getName(), model.getObject().getPrefix()));
}
};
this.add(form);
Button button=new Button("buttonSave");
form.add(button);
// FeedbackPanel //
final FeedbackPanel feedbackPanel = new
JQueryFeedbackPanel("feedback");
form.add(feedbackPanel.setOutputMarkupId(true));
// Auto-complete //
form.add(new AutoCompleteTextField<Country>("autocomplete",
model, new TextRenderer<Country>("name"))
{
private static final long serialVersionUID = 1L;
@Override
protected List<Country> getChoices(String input)
{
List<Country> choices = new ArrayList<Country>();
int count = 0;
for (Country genre: COUNTRIES)
{
if
(genre.getName().toLowerCase().contains(input.toLowerCase()))
{
choices.add(genre);
if (++count == 20) { break; } //limits the
number of results
}
}
return choices;
}
@Override
protected void onSelected(AjaxRequestTarget target)
{
// Markus everything is fine here !
//Country genre = this.getModelObject(); //can be used
info(String.format("Selected country: %s (+%d)",
model.getObject().getName(), model.getObject().getPrefix()));
target.add(feedbackPanel);
}
});
}
// List of Countries //
static final List<Country> COUNTRIES = Arrays.asList(
new Country(33, "France"),
new Country(49, "Allemagne"));
// Bean //
static class Country implements IClusterable
{
private static final long serialVersionUID = 1L;
private final int prefix;
private final String name;
public Country(final int prefix, final String name)
{
this.prefix = prefix;
this.name = name;
}
public int getPrefix()
{
return this.prefix;
}
public String getName()
{
return
this.name;
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HTML:
<!DOCTYPE html>
<html xmlns:wicket="
http://wicket.apache.org">
<head>
<wicket:head>
<title>Wicket - jQuery UI: test page</title>
<style type="text/css">
.ui-autocomplete {
max-height: 200px;
overflow-y: auto;
overflow-x: hidden;
padding-right: 20px;
}
</style>
</wicket:head>
</head>
<body>
<wicket:extend>
<div id="wrapper-panel-frame" class="ui-corner-all">
<form wicket:id="form">
<div>Select a country: (containing any char)</div>
<br/>
<input wicket:id="autocomplete" type="text" size="30"></
input><br/>
<br/>
<div wicket:id="feedback" style="width: 360px;"></div>
<br/>
<button class="saveButton" type="submit"
wicket:id="buttonSave" ><wicket:message key="button.save"/></button>
</form>
</div>
</wicket:extend>
</body>
</html>