Implementing the delete operation

Let's implement the delete CRUD operation using a different approach. Instead of simply adding one single button for the operation, we'll add a delete button on each row in the Grid. The simplest way of adding a UI component inside a Grid is by using the addComponentColumn method:

public class CustomCrud extends Composite {
...

private void initLayout() {
...

grid.addComponentColumn(user -> new Button("Delete", e -> deleteClicked(user)));
...
}
...

private void deleteClicked(User user) {
showRemoveWindow(user);
refresh();
}

private void showRemoveWindow(User user) {
Window window = new RemoveWindow(user);
window.setModal(true);
window.center();
getUI().addWindow(window);
}
}

The addComponentColumn method accepts a ValueProvider used to get a UI component. The constructor used to create the Button accepts a click listener that, in turn, calls the showRemoveWindow method, passing the User instance corresponding to the row where the button resides. The actual implementation of the RemoveWindow class is left as an exercise.

The addComponentColumn method is a shortcut to addColumn(user -> new Button("Delete", e -> deleteClicked(user)), new ComponentRenderer()).
..................Content has been hidden....................

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