Customizing selectable options
Any entity or enum property will by default be rendered via an OptionsFormElementBuilder
resulting in either a select box or list of checkboxes being rendered.
You can customize the type of options control to be generated by setting the viewElementType for a property.
entities.withType( WebPage.class )
.createOrUpdateFormView( fvb -> fvb
/**
* Render the state as radio buttons instead of a select box.
*/
.properties( props -> props
.property( "state" )
.viewElementType( ViewElementMode.CONTROL, BootstrapUiElements.RADIO )
)
);
If no viewElementType has been specified, a default type will be determined: a select box will be used in case of a single value, a checkbox list in case of multiple values.
A select control being generated will be a bootstrap-select with default configuration.
You can customize the select box configuration by manually setting a SelectFormElementConfiguration
attributes.
See the BootstrapUiModule documentation for all configurable properties.
If no viewElementType has been specified, but a SelectFormElementConfiguration
attribute is present, the resulting control will be a select box.
You can manipulate the options that can be selected in several ways by setting either EntityConfiguration
or EntityPropertyDescriptor
attributes.
If your property is another entity type, by default the selectable options will be all entities of that type.
If you want to change this for all properties of that type, you can set either an OptionGenerator.class
, OptionIterableBuilder.class
or EntityAttributes.OPTIONS_ENTITY_QUERY
attribute on the target EntityConfiguration
.
If you want to change it only for a single property, you can configure the same attributes on the EntityPropertyDescriptor
of that property.
entities.withType( WebCmsArticle.class )
.createOrUpdateFormView( fvb -> fvb
/**
* Only allow published sections to be selectable,
* by specifying an EQL statement to fetch them.
*/
.properties( props -> props
.property( "section" )
.attribute( EntityAttributes.OPTIONS_ENTITY_QUERY, "published = TRUE ORDER BY name ASC" )
)
);
When dealing with an enum type, you can also configure the EntityAttributes.OPTIONS_ALLOWED_VALUES
with the `EnumSet`of selectable options.
/**
* Limit the selectable enum HTTP status.
*/
entities.withType( WebCmsUrl.class )
.properties(
props -> props
.property( "httpStatus" )
.attribute(
EntityAttributes.OPTIONS_ALLOWED_VALUES,
EnumSet.of( HttpStatus.OK, HttpStatus.NOT_FOUND )
)
);
Depending on the attribute you will change more of the default behaviour and will have to provide custom implementations. Use the most appropriate attribute for your use case. See the appendix for more information on the different attributes. |