Table of contents
1.
Introduction
2.
CRUD
3.
Columns
4.
Editor
4.1.
Opening the Editor
4.2.
Editor Position
4.2.1.
Overlay (Default)
4.2.2.
🍁 Aside
4.2.3.
🍁 Bottom
4.3.
Editor Content
4.3.1.
🍁 Editor Actions
5.
Sorting and Filtering
5.1.
Disabling Sorting and Filtering
6.
Grid Replacement
7.
Toolbar
7.1.
Hiding the Toolbar
8.
Item Initialization
9.
Frequently Asked Questions
9.1.
Is the Vaadin application scalable?
9.2.
Is the Vaadin application stateless?
9.3.
What do you know about Vaadin flow?
10.
Conclusion
Last Updated: Mar 27, 2024
Medium

Vaadin - CRUD

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

CRUD (Create, Read, Update, Delete) is an acronym for the various operations that can be performed on stored data. It is a shorthand for the four fundamental functions of persistent storage. The Vaadin CRUD is the data management component of Vaadin flow.

In this article, we will discuss Vaadin CRUD. It will include various Vaadin CRUD application operations on columns, editor, grid replacement, and toolbar. Afterward, we will discuss sorting and filtering, item initialization, and localization on the Vaadin database, thus concluding our discussion of Vaadin CRUD. So let’s get started!
 

Vaadin CRUD Intro img

CRUD

Vaadin CRUD is a dataset management component. It makes it simple to create, read (display), edit (update), and delete items.

crud = new Crud<>(
  Person.class,
  createEditor()
);

setupGrid();
setupDataProvider();

add(crud);

 

CRUD Image

Columns

Vaadin CRUD generates columns for each field in the provided dataset automatically. You can add new columns as well as modify or remove existing ones.

crud = new Crud<>(
  Person.class,
  createEditor()
);

setupGrid();
setupDataProvider();

add(crud);
// Only show these columns (all columns shown by default):
List<String> visibleColumns = Arrays.asList(
  FIRST_NAME,
  LAST_NAME,
  PROFESSION,
);
grid.getColumns().forEach(column -> {
  String key = column.getKey();
  if (!visibleColumns.contains(key)) {
    grid.removeColumn(column);
  }
});

 

CRUD Column

Editor

The editor UI of Vaadin CRUD is used to edit data.

Opening the Editor

The editor is opened by default by clicking the edit button in the last column, but this button column can be removed if you want to provide another way to engage with the editor. You can, for example, open it with a double click:

Grid<Person> grid = crud.getGrid();

// Remove edit column
Crud.removeEditColumn(grid);
// grid.removeColumnByKey(EDIT_COLUMN);
// grid.removeColumn(grid.getColumnByKey(EDIT_COLUMN));

// Open editor on double-click
grid.addItemDoubleClickListener(event ->
  crud.edit(event.getItem(), Crud.EditMode.EXISTING_ITEM)
);

 

Opening editor

Editor Position

The editor can be placed in an overlay (the default), on the side, or at the bottom of the screen.


Overlay (Default)

The overlay position displays the editor in modal mode. Overlays are not limited by the size of the CRUD, making them ideal for complex forms. They do, however, prevent the user from viewing and interacting with the Grid beneath.


🍁 Aside

The editor appears as an overlay next to the grid in the aside position. When there is enough horizontal space to accommodate both the grid and the editor, and it is advantageous for the user to be able to view and interact with the grid while the editor is open, use this position. Aside positioning works well for single-column forms as well.

crud.setEditorPosition(CrudEditorPosition.ASIDE);


🍁 Bottom

When the user needs to see as many columns in the grid as possible while editing, when horizontal space is limited, or when a wider editor form is desired, the bottom position can be useful.

crud.setEditorPosition(CrudEditorPosition.BOTTOM);

Editor Content

Except for the header and footer, the editor's content is completely customizable.

crud = new Crud<>(
  Person.class,
  createEditor()
);

...

private CrudEditor<Person> createEditor() {
  TextField firstName = new TextField("First name");
  TextField lastName = new TextField("Last name");
  EmailField email = new EmailField("Email");
  ComboBox<String> profession = new ComboBox<>("Profession");
  profession.setItems(professions);


  FormLayout form = new FormLayout(firstName, lastName, email, profession);
  form.setColspan(email, 2);
  form.setColspan(profession, 2);
  form.setMaxWidth("480px");
  form.setResponsiveSteps(
    new FormLayout.ResponsiveStep("0", 1),
    new FormLayout.ResponsiveStep("30em", 2)
  );

  Binder<Person> binder = new Binder<>(Person.class);
  binder.forField(firstName).asRequired().bind(Person::getFirstName, Person::setFirstName);
  binder.forField(lastName).asRequired().bind(Person::getLastName, Person::setLastName);
  binder.forField(email).asRequired().bind(Person::getEmail, Person::setEmail);
  binder.forField(profession).asRequired().bind(Person::getProfession, Person::setProfession);

  return new BinderCrudEditor<>(binder, form);
}

 

Edit Item

🍁 Editor Actions

There are three buttons in the editor:

  • "Delete" displays a confirmation dialogue asking the user to confirm that they want to delete the item.
     
  • "Cancel" closes the editor unless there are any unsaved changes, in which case a confirmation dialogue appears and the user can choose to discard the changes or continue editing.
     
  • The "Save" button saves the changes and closes the editor (disabled until a change is made).

Sorting and Filtering

CRUD supports sorting and filtering of any column by default.

Disabling Sorting and Filtering

We have a method by which we can disable sorting and filtering.

crud = new Crud<>(
  Person.class,
  createGrid(),
  createEditor()
);

...

private CrudGrid<Person> createGrid() {
  // Create the new CrudGrid to disable filtering (last boolean parameter)
  CrudGrid<Person> grid = new CrudGrid<>(Person.class, false);

  // Disable sorting
  grid.setSortableColumns();

...

return grid;
}

Grid Replacement

The default Grid in CRUD can be changed. This is useful if you want to customise the Grid, such as putting the edit Button in the first column.

crud = new Crud<>(
  Person.class,
  createGrid(),
  createEditor()
);

...

private Grid<Person> createGrid() {
  Grid<Person> grid = new Grid<>();
  Crud.addEditColumn(grid);
  grid.addColumn(Person::getFirstName).setHeader("First name");
  grid.addColumn(Person::getLastName).setHeader("Last name");
  grid.addColumn(Person::getProfession).setHeader("Profession");
  return grid;
}

 

Grid replacement

Toolbar

The "New item" button in the CRUD toolbar is used to create new items. The toolbar and its Button can both be customised. We can use the toolbar to display statistics such as the size of the dataset and the number of search results, for example.

toolbar
Html total = new Html("<span>Total: <b>" + dataProvider.DATABASE.size() + "</b> employees</span>");

Button button = new Button("New employee", VaadinIcon.PLUS.create());
button.addClickListener(event -> {
  crud.edit(new Person(), Crud.EditMode.NEW_ITEM);
});
button.addThemeVariants(ButtonVariant.LUMO_TERTIARY);

HorizontalLayout toolbar = new HorizontalLayout(total, button);
toolbar.setAlignItems(FlexComponent.Alignment.CENTER);
toolbar.setFlexGrow(1, toolbar);
toolbar.setJustifyContentMode(FlexComponent.JustifyContentMode.BETWEEN);
toolbar.setSpacing(false);

crud.setToolbar(toolbar);

 

toolbar crud

Hiding the Toolbar

If not required, the toolbar can be hidden.

crud = new Crud<>(
  Person.class,
  createEditor()
);
crud.setToolbarVisible(false);

Item Initialization

Data can be added to newly created items.

crud = new Crud<>(
  Person.class,
  createEditor()
);
crud.addNewListener(event -> {
  Person person = event.getItem();
  person.setEmail("@vaadin.com");
  person.setProfession("Developer");
  crud.getEditor().setItem(person);
});

 

item initialization

Frequently Asked Questions

Is the Vaadin application scalable?

To summarise, Vaadin applications scale well. As with any framework, mistakes are possible. However, by adhering to standard Java best practices, it is simple to create applications with Vaadin that not only scale well but also look great and work well, making your users happy.

Is the Vaadin application stateless?

From what we had known from Vaadin Fusion, the fusion does not create server sessions by default and instead employs the token-based authentication mechanism. The server is then stateless, allowing for easier horizontal scaling of instances and high service availability.

What do you know about Vaadin flow?

Vaadin Flow is a one-of-a-kind framework that allows you to create web apps without having to write HTML or JavaScript.

Conclusion

In this article, we have extensively discussed the Vaadin CRUD, including various Vaadin CRUD applications, such as columns, editor, grid replacement, and toolbar. Afterward, we discussed sorting and filtering, and item initialization, thus concluding our discussion of Vaadin CRUD.

If you think this blog has helped you with Vaadin CRUD, and if you would like to learn more, check out our articles Vaadin GridVaadin Environment SetupVaadin Architecture, and Vaadin Confirm Dialog. You can also refer to our guided path on the basics of java and many more on our Website.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 

But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass