Buy me a coffee :)

Wicket - jQuery UI

jQuery UI integration in Wicket 6.x, Wicket 7.x , Wicket 8.x & Wicket 9.x

Auto-complete: custom beans

Select a metal genre: (containing any char)

Components / Samples

Description

The AutoCompleteTextField component allows you to search against a bean list.

Important

As the suggestion list is displayed as text, the bean should either override #toString() or use a ITextRenderer (like TextRenderer). If you plan to use AutoCompleteUtils helper class, the override of #toString() is mandatory.

Sources

package com.googlecode.wicket.jquery.ui.samples.jqueryui.autocomplete;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.image.ContextImage;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.googlecode.wicket.jquery.core.resource.StyleSheetPackageHeaderItem;
import com.googlecode.wicket.jquery.core.utils.ListUtils;
import com.googlecode.wicket.jquery.ui.form.autocomplete.AutoCompleteTextField;
import com.googlecode.wicket.jquery.ui.samples.data.bean.Genre;
import com.googlecode.wicket.jquery.ui.samples.data.dao.GenresDAO;

public class CustomAutoCompletePage extends AbstractAutoCompletePage
{
	private static final long serialVersionUID = 1L;

	public CustomAutoCompletePage()
	{
		// Model //
		final IModel<Genre> model = Model.of(GenresDAO.newGenre());

		// Form //
		final Form<Void> form = new Form<Void>("form");
		this.add(form);

		// Container for selected genre (name & cover) //
		final WebMarkupContainer container = new WebMarkupContainer("container");
		form.add(container.setOutputMarkupId(true));

		container.add(new ContextImage("cover", new PropertyModel<String>(model, "cover")));
		container.add(new Label("name", new PropertyModel<String>(model, "name")));

		// Auto-complete //
		form.add(new AutoCompleteTextField<Genre>("autocomplete", model) {

			private static final long serialVersionUID = 1L;

			@Override
			protected List<Genre> getChoices(String input)
			{
				return ListUtils.contains(input, GenresDAO.all());
			}

			@Override
			protected void onSelected(AjaxRequestTarget target)
			{
				target.add(container); // the model has already been updated
			}
		});
	}

	// Methods //

	@Override
	public void renderHead(IHeaderResponse response)
	{
		super.renderHead(response);

		response.render(new StyleSheetPackageHeaderItem(CustomAutoCompletePage.class));
	}
}
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<wicket:head>
	<title>Wicket jQuery UI: auto-complete (custom bean)</title>
</wicket:head>
</head>
<body>
<wicket:extend>
	<div id="demo-panel">
		<form wicket:id="form">
			<table>
				<tr>
					<td style="width: 300px; vertical-align: top;">
						<div>Select a metal genre: (containing any char)</div>
						<br/>
						<input wicket:id="autocomplete" type="text" size="30" />
					</td>
					<td wicket:id="container" style="width: 100px;">
						<img wicket:id="cover" class="ui-corner-all" />
						<div wicket:id="name" style="text-align: center;"></div>
					</td>
				</tr>
			</table>
		</form>
	</div>
</wicket:extend>
</body>
</html>
table {
	width: 400px;
	border-spacing: 0;
	border-collapse: collapse;
}

th, td {
	padding: 5px;
}

.ui-autocomplete {
	max-height: 200px;
	overflow-y: auto;
	overflow-x: hidden;
	padding-right: 20px;
}