java.lang.Object
org.wicketstuff.select2.ChoiceProvider<T>
- Type Parameters:
T- type of choice object
- All Implemented Interfaces:
Serializable,IDetachable,IClusterable
- Direct Known Subclasses:
StringTextChoiceProvider
Acts as a bridge between Select2 components and the application's domain model.
The two important services provided by implementations are:
- Feeding choices, represented by the application's domain objects, to Select2 components
- Transcoding said choices to and from Json so they can consumed by Select2
For the most common usecase where each choice is rendered as a text string see
StringTextChoiceProvider.
- Author:
- igor
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddetach()abstract StringgetDisplayValue(T object) Get the value for displaying to an end user.abstract StringgetIdValue(T object) This method is called to get the id value of an object (used as the value attribute of a choice element) The id can be extracted from the object like a primary key, or if the list is stable you could just return a toString of the index.abstract voidQueries application for choices that match the searchtermand adds them to theresponseabstract Collection<T>toChoices(Collection<String> ids) Converts a list of choice ids back into application's choice objects.protected voidConverts the specified choice to Json.
-
Constructor Details
-
ChoiceProvider
public ChoiceProvider()
-
-
Method Details
-
getDisplayValue
Get the value for displaying to an end user.- Parameters:
object- the actual object- Returns:
- the value meant for displaying to an end user
-
getIdValue
This method is called to get the id value of an object (used as the value attribute of a choice element) The id can be extracted from the object like a primary key, or if the list is stable you could just return a toString of the index.Note that the given index can be
-1if the object in question is not contained in the available choices.- Parameters:
object- The object for which the id should be generated- Returns:
- String
-
query
Queries application for choices that match the searchtermand adds them to theresponse- Parameters:
term- search termpage- requested search term results pageresponse- aggregate for matching choices as well as other response options
-
toJson
protected void toJson(T choice, com.github.openjson.JSONStringer stringer) throws com.github.openjson.JSONException Converts the specified choice to Json.At the very least each choice should contain an
idattribute. If no custom rendering function is specified, thetextattribute should also be providedExample: If mapping a User {Long id, String name} using default rendering the code should look like this:
toJson(User choice, JSONWriter writer) { writer.key("id").value(choice.getId()).key("text").value(choice.getName()); }- Parameters:
choice- choice to convertstringer- Json writer that should be used to covnert the choice- Throws:
com.github.openjson.JSONException
-
toChoices
Converts a list of choice ids back into application's choice objects. When the choice provider is attached to a single-select component theidscollection will contain exactly one id, and a collection containing exactly one choice should be returned.- Parameters:
ids- collection containing choice ids- Returns:
- collection containing application choice objects
-
detach
public void detach()- Specified by:
detachin interfaceIDetachable
-