alejandro-du / crudui

Automatically generate CRUD-like Vaadin views for any Java Bean
https://vaadin.com/directory#!addon/crud-ui-add-on
Apache License 2.0
86 stars 54 forks source link

Dynamically changing CRUD operations #81

Closed envas closed 3 years ago

envas commented 3 years ago

I have the requirement to dynamically enable/disable CRUD operations depending on the data. Example: a closed contract cannot be deleted. I would like to manage this in the frontend and not later in the backend or even in the database. My idea was to add another item click listener to the grid managing the crud header actions, for example:

crud.getGrid().addItemClickListener(e -> {
                if (e.getItem().getContracStatus() == CLOSED) {
                    crud.getDeleteButton().setEnabled(false);
                } else {
                    crud.getDeleteButton().setEnabled(true);
                }
            });

Works fine, however, if editing the object is still allowed there is a problem on edit because the itemClickEvent is no more raised and after the update transaction finishes the delete button becomes active.

Any idea for a better solution? It would be nice if we could access the header objects from a custom form factory because buildNewForm is IMO the place where the form dynamic occurs. It's a bit confusing that the action buttons Save and Cancel are part of the form and instantiated by the form factory and the action buttons Add, Delete and Edit are part of the crud package. IMO all the actions belong to business logic and should be accessible from the same logical unit, ether crud or form.

alejandro-du commented 3 years ago

You need to also update the Crud after the update operation.