Crud UI Add-on
Generate CRUD UIs for your entities/beans/POJOs at runtime
Crud UI Add-on provides an API to automatically generate CRUD-like UIs for any Java Bean at runtime.
The API is defined through 4 interfaces:
CrudComponent
: A Vaadin Component
that can be added to any ComponentContainer
. This is the actual CRUD final users will see in the browser.
CrudListener
: Encapsulates the CRUD operations. You can implement this interface to delegate CRUD operations to your back-end.
CrudLayout
: Encapsulates layout-related behavior.
CrudFormFactory
: Builds the forms required by the CRUD UI.
The add-on includes several implementations of these interfaces.
Basic usage
Say, you have the following domain/entity/Java Bean class:
public class User {
@NotNull // Validation API is required! Add it as a dependency on your project
private Long id;
@NotNull
private String name;
private Date birthDate;
@Email
private String email;
@NotNull
private String password;
... getters & setters ...
}
You can create a new CRUD component and add it into any Vaadin layout as follows:
GridCrud<User> crud = new GridCrud<>(User.class);
layout.addComponent(crud);
You can enable Java Bean Validation as follows:
crud.getCrudFormFactory().setUseBeanValidation(true);
Use lambda expressions or method references to delegate CRUD operations to your backend:
crud.setFindAllOperation(() -> backend.findAll());
crud.setAddOperation(backend::add);
crud.setUpdateOperation(backend::update);
crud.setDeleteOperation(backend::delete);
Advanced usage
As an alternative to method references and lambda expressions, you can implement a CrudListener
to delegate CRUD operations to your backend:
crud.setCrudListener(new CrudListener<User>() {
@Override
public Collection<User> findAll() {
return backend.findAllUsers();
}
@Override
public User add(User user) {
return backend.add(user);
}
@Override
public User update(User user) {
return backend.update(user);
}
@Override
public void delete(User user) {
backend.remove(user);
}
});
Use a different CrudLayout
implementation:
GridCrud<User> crud = new GridCrud<>(User.class, new HorizontalSplitCrudLayout());
Set a custom CrudFormFactory
:
CustomCrudFormFactory<User> formFactory = new CustomCrudFormFactory<>(User.class);
crud.setCrudFormFactory(formFactory);
Configure form fields visibility:
formFactory.setVisibleProperties(CrudOperation.READ, "name", "birthDate", "email", "groups", "mainGroup", "active");
formFactory.setVisibleProperties(CrudOperation.ADD, "name", "birthDate", "email", "password", "groups", "mainGroup", "active");
formFactory.setVisibleProperties(CrudOperation.UPDATE, "name", "birthDate", "email", "groups", "mainGroup", "active");
formFactory.setVisibleProperties(CrudOperation.DELETE, "name", "email");
Use nested properties in GridCrud
instances:
crud.getGrid().addColumn(user -> user.getMainGroup().getName()).setHeader("Main group").setKey("key");
Configure the type of an input field:
formFactory.setFieldType("password", PasswordField.class);
Customize fields after their creation:
formFactory.setFieldCreationListener("birthDate", field -> ... your own logic here ...);
Define a FieldProvider
to manually create a field:
formFactory.setFieldProvider("groups", user -> {
CheckboxGroup<Group> checkboxes = new CheckboxGroup<>();
checkboxes.setItems(groups);
checkboxes.setItemLabelGenerator(Group::getName);
return checkboxes;
});
Or use one of the included FieldProvider
implementations:
formFactory.setFieldProvider("groups",
new CheckBoxGroupProvider<>("Groups", GroupRepository.findAll(), Group::getName));
Set a Converter
:
formFactory.setConverter("salary", new Converter<String, BigDecimal>() {
@Override
public Result<BigDecimal> convertToModel(String value, ValueContext valueContext) {
return Result.ok(new BigDecimal(value)); // error handling omitted
}
@Override
public String convertToPresentation(BigDecimal value, ValueContext valueContext) {
return value.toPlainString();
}
});
Customize captions:
formFactory.setButtonCaption(CrudOperation.ADD, "Add new user");
crud.setRowCountCaption("%d user(s) found");
Links
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
Requires Java 11 and Vaadin 23.1.3. Fixes base FieldProvider generics
Thanks to Boniface Chacha for the contributions.
- Released
- 2022-08-02
- Maturity
- STABLE
- License
- Apache License 2.0
Compatibility
- Framework
- Vaadin 23+
- Vaadin 7.5+ in 1.2
- Vaadin 8.1+ in 2.1.0
- Vaadin 8.0+ in 2.0.0
- Vaadin 8.2+ in 2.3.0
- Vaadin 10 in 3.1.0
- Vaadin 8.6+ in 2.3.1
- Vaadin 13+ in 3.8.0
- Vaadin 13 in 3.9.0
- Vaadin 14+ in 4.0.0
- Vaadin 18+ in 4.5.0
- Vaadin 21+ in 5.0.0
- Vaadin 20+ in 4.6.0
- Vaadin 10+ in 3.0
- Vaadin 24+ in 7.0.0
- Browser
- Browser Independent