Implementing the read operation

We can start by configuring the columns we actually want to show in the Grid. Since the columns were automatically created by the constructor, we can set their visibility by name using the setColumns method:

...
private void initLayout() {
...
grid
.setColumns("firstName", "lastName", "email", "mainRole");
...
}
...

In contrast to the previous editable Grid, we don't need the Password column here, since we are not using an Editor.

We can continue by adding a click listener to the refresh button, and implementing the refresh method. This is pretty straightforward:

...
private void initBehavior() {
grid.asSingleSelect().addValueChangeListener(e -> updateHeader());
refresh.addClickListener(e -> refresh());
}

public void refresh() {
grid.setItems(UserRepository.findAll());
updateHeader();

}

private void updateHeader() {
boolean selected = !grid.asSingleSelect().isEmpty();
edit.setEnabled(selected);
}
...

We introduced a new updateHeader method to enable or disable the edit button depending on the selection state in the Grid. It makes sense to have the edit button enabled only when there's a row selected. We need to call this method when we refresh the list and when the value selected in the Grid changes (see the Grid.addValueChangeListener method).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset