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 template

Select a metal genre: (containing any char)



Components / Samples

Description

AutoCompleteTextField widget allows you to customize the item's display using a jQuery template, which will be enclosed in a <script type="text/x-jquery-tmpl" /> (so you can use the "\n" character to properly format the template).

For an AutoComplete, the properties used in the template text (ie: #: data.name #) should be prefixed by "data." and should be identified in the list returned by IJQueryTemplate#getTextProperties(), without "data.".

@Override
protected IJQueryTemplate newTemplate()
{
	return new IJQueryTemplate() {

		private static final long serialVersionUID = 1L;

		@Override
		public String getText()
		{
			return  "<i>#: data.name #</i>";
		}

		@Override
		public List getTextProperties()
		{
			// should be specified, unless already part of the ITextRenderer
			return Arrays.asList("name"); 
		}
	};
}

Since

wicket-jquery-ui-core-1.2.0

Sources

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

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.head.IHeaderResponse;
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.core.resource.StyleSheetPackageHeaderItem;
import com.googlecode.wicket.jquery.core.template.IJQueryTemplate;
import com.googlecode.wicket.jquery.core.utils.ListUtils;
import com.googlecode.wicket.jquery.ui.form.autocomplete.AutoCompleteTextField;
import com.googlecode.wicket.jquery.ui.panel.JQueryFeedbackPanel;
import com.googlecode.wicket.jquery.ui.samples.data.bean.Genre;
import com.googlecode.wicket.jquery.ui.samples.data.dao.GenresDAO;

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

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

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

		// FeedbackPanel //
		final FeedbackPanel feedback = new JQueryFeedbackPanel("feedback");
		form.add(feedback.setOutputMarkupId(true));

		// 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)
			{
				info("Your favorite rock genre is: " + this.getModelObject());
				target.add(feedback);
			}

			@Override
			protected IJQueryTemplate newTemplate()
			{
				return new IJQueryTemplate() {

					private static final long serialVersionUID = 1L;

					/**
					 * The template text will be enclosed in a <script type="text/x-jquery-tmpl" />.
					 * You can use the "\n" character to properly format the template.
					 */
					@Override
					public String getText()
					{
						return	"<table style='width: 100%' cellspacing='0' cellpadding='0'>\n" +
								" <tr>\n" +
								"  <td><img src='${coverUrl}' width='50px' /></td>\n" +
								"  <td>${name}</td>\n" +
								" </tr>\n" +
								"</table>";
					}

					@Override
					public List<String> getTextProperties()
					{
						return Arrays.asList("name", "coverUrl");
					}

				};
			}
		});
	}

	// Methods //

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

		response.render(new StyleSheetPackageHeaderItem(TemplateAutoCompletePage.class));
	}
}
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<wicket:head>
	<title>Wicket jQuery UI: auto-complete (custom template)</title>
</wicket:head>
</head>
<body>
<wicket:extend>
	<div id="demo-panel">
		<form wicket:id="form">
			<div>Select a metal genre: (containing any char)</div>
			<br/>
			<input wicket:id="autocomplete" type="text" size="30" /><br/>
			<br/>
			<div wicket:id="feedback" style="width: 360px;"></div>
		</form>
	</div>
</wicket:extend>
</body>
</html>
.ui-autocomplete {
	max-height: 220px;
	overflow-y: auto;
	overflow-x: hidden;
	padding-right: 20px;
}