Crud UI Add-on - Vaadin Add-on Directory
Generate CRUD UIs for your entities/beans/POJOs at runtimeCrud 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:
```java
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:
```java
GridCrud crud = new GridCrud<>(User.class);
layout.addComponent(crud);
```
You can enable _Java Bean Validation_ as follows:
```java
crud.getCrudFormFactory().setUseBeanValidation(true);
```
Use lambda expressions or method references to delegate CRUD operations to your backend:
```java
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:
```java
crud.setCrudListener(new CrudListener() {
@Override
public Collection 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:
```java
GridCrud crud = new GridCrud<>(User.class, new HorizontalSplitCrudLayout());
````
Set a custom `CrudFormFactory`:
```java
CustomCrudFormFactory formFactory = new CustomCrudFormFactory<>(User.class);
crud.setCrudFormFactory(formFactory);
```
Configure form fields visibility:
```java
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:
```java
crud.getGrid().addColumn(user -> user.getMainGroup().getName()).setHeader("Main group").setKey("key");
```
Configure the type of an input field:
```java
formFactory.setFieldType("password", PasswordField.class);
```
Customize fields after their creation:
```java
formFactory.setFieldCreationListener("birthDate", field -> ... your own logic here ...);
```
Define a `FieldProvider` to manually create a field:
```java
formFactory.setFieldProvider("groups", user -> {
CheckboxGroup checkboxes = new CheckboxGroup<>();
checkboxes.setItems(groups);
checkboxes.setItemLabelGenerator(Group::getName);
return checkboxes;
});
```
Or use one of the included `FieldProvider` implementations:
```java
formFactory.setFieldProvider("groups",
new CheckBoxGroupProvider<>("Groups", GroupRepository.findAll(), Group::getName));
```
Set a `Converter`:
````java
formFactory.setConverter("salary", new Converter() {
@Override
public Result 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:
```java
formFactory.setButtonCaption(CrudOperation.ADD, "Add new user");
crud.setRowCountCaption("%d user(s) found");
```