AutoSuggest elements
An AutoSuggestFormElement
writes a <div>
tag to the output containing all markup for an autoSuggest control.
Element and builder
The AutoSuggestFormElement
has an equivalent AutoSuggestFormElementBuilder
.
A builder can be created using BootstrapViewElements.bootstrap.builders.autosuggest()
factory methods.
By default, the autosuggest form element expects an id
and a value
property on the fetched results.
The id
is the reference to the domain object, and the value
is the value that is displayed by default.
AutoSuggestFormElementConfiguration
The autosuggest control takes in an AutoSuggestFormElementConfiguration
to configure a datasets and has the following options:
Method | Description |
---|---|
|
Configure the dataset with the given name, if it is not yet present, it will be added first. |
|
Should the textbox show a hint for auto-completion. |
|
Highlight the matching input text in the suggestions. |
|
Minimum length the input value should have before fetching suggestion. |
import static com.foreach.across.modules.bootstrapui.ui.factories.BootstrapViewElements.bootstrap;
bootstrap.builders
.autosuggest()
.configuration(
AutoSuggestFormElementConfiguration.withDataSet( (1)
dataSet -> dataSet.remoteUrl( "/bootstrapAutosuggest/suggest?query={{query}}" )
).minLength( 2 )
)
.build()
1 | configure a dataset which fetches data from an url, once at least 2 characters are present |
Configuring templates
An AutoSuggestFormElementBuilder
supports configuring templates by specifying a ViewElementBuilder
for the given template.
Depending on the configuration, different objects are available within the context of the template.
These can be specified with curly braces, for example {{query}}
, within the template and will be interpolated when the element is rendered.
The following templates can be configured:
Template name | Description |
---|---|
header |
Rendered at the top of the dataset when suggestions are present.
In the rendering context, the |
footer |
Rendered at the top of the dataset when suggestions are present.
In the rendering context, the |
pending |
Rendered when there are no synchronous suggestions available, but asynchronous suggestions are being fetched.
In the rendering context the |
suggestion |
Used to render a single suggestion.
The associated suggestion object will be available in the context.
If no template is configured, the default template |
notFound |
Rendered when no suggestions could be found for the current query.
In the rendering context the |
When configuring a template, optionally a dataset id can be specified for which the template should be added.
import static com.foreach.across.modules.bootstrapui.ui.factories.BootstrapViewElements.bootstrap;
bootstrap.builders
.autosuggest()
.notFoundTemplate( bootstrap.builders.div().css( "autosuggest-not-found" )
.add( bootstrap.builders.text( "There are no results available for '{{query}}'" ) ) )
.build();
Given the query 'John', the following markup would be rendered in the suggestions dropdown if there are no results:
<div class="autosuggest-not-found">There are no results available for 'John'</div>
import static com.foreach.across.modules.bootstrapui.ui.factories.BootstrapViewElements.bootstrap;
bootstrap.builders
.autosuggest()
.notFoundTemplate( "people", bootstrap.builders.text("No results found") )
.build();
Dynamic configuration of a dataset
An autosuggest form element supports clientside modification of the active dataset(s).
On the autosuggest element itself an attribute datasets
is available which holds the configured Bloodhound search engines.
By accessing the datasets, the urls used to fetch the data can be modified:
$( '[data-bootstrapui-adapter-type="autosuggest"]' ) (1)
.data( 'datasets' )['default'].remote.url = '/active-users'; (2)
1 | Select the autosuggest for which you want to modify a dataset. In this case, we’re selecting an autosuggest form element by its corresponding control adapter. |
2 | Find the dataset by name and reconfigure the remote url. Now the new url will be used when data is fetched. |