com.databasesandlife.util.wicket
Class MultipleValueAutoSuggestTextField
java.lang.Object
org.apache.wicket.Component
org.apache.wicket.MarkupContainer
org.apache.wicket.markup.html.WebMarkupContainer
org.apache.wicket.markup.html.form.LabeledWebMarkupContainer
org.apache.wicket.markup.html.form.FormComponent<T>
org.apache.wicket.markup.html.form.FormComponentPanel<String[]>
com.databasesandlife.util.wicket.MultipleValueAutoSuggestTextField
- All Implemented Interfaces:
- Serializable, IClusterable, IConverterLocator, IFormModelUpdateListener, IFormVisitorParticipant, ILabelProvider<String>, IHeaderPartContainerProvider
public class MultipleValueAutoSuggestTextField
- extends FormComponentPanel<String[]>
Represents a text-field in Wicket, which allows the user to enter multiple values, and values are suggested from a list of existing values. These values are only suggestions, however, the user may type in other new values, not in the list of existing values. This is ideal for entering tags on a newly created object, where you want the user to use existing tags if appropriate, but also allow the user to create new tags if none of the existing ones are appropriate.

If new values should not be allowed, for example when selecting from a list of existing languages, see MultipleValueAutoCompleteTextField.
The model of the field is an array of Strings which have been chosen. (It is not possible to display strings to the user, while maintaining a list of IDs corresponding to those strings internally, as the user may enter new strings, and those would have no corresponding IDs.)
The list of values to be suggested is a collection of Strings. There are two ways the these may be fetched:
- Client-side - Either all entries are given to the object as a String[], in which case they will be inserted into the HTML page. Suggestions are fast, as a server round-trip is not necessary. This is not practical if there are 1M entries, as the generated HTML will be too large.
- Server-side - Or a lookup function is provided which can take a substring and can return an ordered String[] of suggestions matching that substring, it is advised to return no more than 10 or 50 entries from this function, otherwise the HTTP communication will be too large. This requires a server round-trip each time the user types a character, so is less responsive.
Usage:
<!-- in HTML -->
<input type="text" wicket:id="tags" class="my-css-class"></input>
// In Java
MultipleValueAutoSuggestTextField tagsField =
new MultipleValueAutoSuggestTextField("tags");
tagsField.setClientSideOptions(new String[] { "java", "php" }); // or..
tagsField.setServerSideDataSource(new AutoSuggestDataSource() {
public String[] suggest(String userEnteredPartialText) {
return new String[] { "java", "php" };
}
});
form.add(tagsField);
If there is an error about a missing </span> tag, make sure the <input> tag is closed with a </input>, even though HTML would not normally require it to be closed. This is a consequence of an implementation issue.
The Javascript used by this software is based on the JQuery autocomplete multiple example.
- Version:
- $Revision: 2662 $
- Author:
- The Java source is copyright Adrian Smith and licensed under the LGPL 3.
- See Also:
- Serialized Form
| Methods inherited from class org.apache.wicket.markup.html.form.FormComponent |
add, add, clearInput, error, getConvertedInput, getForm, getInput, getInputAsArray, getInputName, getModel, getModelObject, getRawInput, getType, getValidatorKeyPrefix, getValidators, getValue, hasRawInput, inputChanged, invalid, isInputNullable, isMultiPart, isPersistent, isRequired, isValid, newValidatable, processChildren, processInput, setConvertedInput, setLabel, setModel, setModelObject, setModelValue, setModelValue, setPersistent, setRequired, setType, updateModel, valid, validate, visitComponentsPostOrder, visitFormComponentsPostOrder |
| Methods inherited from class org.apache.wicket.MarkupContainer |
add, addOrReplace, autoAdd, autoAdd, contains, get, get, getAssociatedMarkupStream, getMarkupStream, hasAssociatedMarkup, internalAdd, isTransparentResolver, iterator, iterator, newMarkupResourceStream, remove, remove, removeAll, renderAssociatedMarkup, replace, setDefaultModel, size, swap, toString, toString, visitChildren, visitChildren |
| Methods inherited from class org.apache.wicket.Component |
add, afterRender, beforeRender, configure, continueToOriginalDestination, debug, detach, detachBehaviors, detachModels, determineVisibility, error, fatal, findParent, findParentWithAssociatedMarkup, getApplication, getBehaviors, getBehaviorsRawList, getClassRelativePath, getComponentBorder, getConverter, getConverter, getDefaultModel, getDefaultModelObject, getDefaultModelObjectAsString, getDefaultModelObjectAsString, getEscapeModelStrings, getFeedbackMessage, getId, getInnermostModel, getLocale, getLocalizer, getMarkupAttributes, getMarkupId, getMarkupId, getMarkupIdImpl, getMetaData, getModelComparator, getOutputMarkupId, getOutputMarkupPlaceholderTag, getPage, getPageRelativePath, getParent, getPath, getRenderBodyOnly, getRequest, getRequestCycle, getResponse, getSession, getSizeInBytes, getString, getString, getString, getStyle, getVariation, hasBeenRendered, hasErrorMessage, hasFeedbackMessage, info, internalAttach, internalDetach, isActionAuthorized, isAncestorOf, isEnableAllowed, isEnabled, isEnabledInHierarchy, isRenderAllowed, isStateless, isVersioned, isVisibilityAllowed, isVisible, isVisibleInHierarchy, markRendering, modelChanged, modelChanging, prepareForRender, prepareForRender, redirectToInterceptPage, remove, remove, render, render, renderComponent, renderComponent, rendered, replaceWith, sameInnermostModel, sameInnermostModel, setComponentBorder, setDefaultModelObject, setEnabled, setEscapeModelStrings, setMarkupId, setMarkupIdImpl, setMetaData, setOutputMarkupId, setOutputMarkupPlaceholderTag, setRedirect, setRenderBodyOnly, setResponsePage, setResponsePage, setResponsePage, setVersioned, setVisibilityAllowed, setVisible, urlFor, urlFor, urlFor, urlFor, urlFor, urlFor, visitParents, warn |
MultipleValueAutoSuggestTextField
public MultipleValueAutoSuggestTextField(String wicketId)
setClientSideOptions
public MultipleValueAutoSuggestTextField setClientSideOptions(String[] options)
- Returns:
- this
setServerSideDataSource
public MultipleValueAutoSuggestTextField setServerSideDataSource(MultipleValueAutoSuggestTextField.AutoSuggestDataSource dataSource)
- Returns:
- this
setSeparator
public MultipleValueAutoSuggestTextField setSeparator(String separatorForOutput,
String separatorCharacterClassRegexp)
- When the user types multiple options, they will be separated by, for example, ",".
This method sets that separator.
There are two places the separator is used: when generating the text field from a list
of values (separatorForOutput), and when the text field is being parsed into
a list of values (separatorCharacterClassRegexp, which is a regular expression).
- Parameters:
separatorForOutput - For example ", " - plain textseparatorCharacterClassRegexp - For example ",;\\s" - regular expression
- Returns:
- this
getCallInitializerJS
public String getCallInitializerJS()
- Internal method - Do not use