3.5.5.2.12. RemoveAction
RemoveAction is a list action designed to remove entity instances from a data container in UI and from the database.
The action is implemented by com.haulmont.cuba.gui.actions.list.RemoveAction
class and should be defined in XML using type="remove"
action’s attribute. You can configure common action parameters using XML attributes of the action
element, see Declarative Actions for details. Below we describe parameters specific to the RemoveAction
class.
The following parameters can be set both in XML and in Java:
-
confirmation
- boolean value specifying whether to show confirmation dialog before removing the selected entities. True by default. -
confirmationMessage
- confirmation dialog message. By default, it is taken from the main message pack using thedialogs.Confirmation.Remove
key. -
confirmationTitle
- confirmation dialog title. By default, it is taken from the main message pack using thedialogs.Confirmation
key.
For example, if you want to show a specific confirmation message, you can configure the action in XML:
<action id="remove" type="remove">
<properties>
<property name="confirmation" value="true"/>
<property name="confirmationTitle" value="Removing customer..."/>
<property name="confirmationMessage" value="Do you really want to remove the customer?"/>
</properties>
</action>
Alternatively, you can inject the action into the screen controller and configure it using setters:
@Named("customersTable.remove")
private RemoveAction customersTableRemove;
@Subscribe
public void onInit(InitEvent event) {
customersTableRemove.setConfirmation(true);
customersTableRemove.setConfirmationTitle("Removing customer...");
customersTableRemove.setConfirmationMessage("Do you really want to remove the customer?");
}
Now let’s consider parameters that can be configured only in Java code. In order to generate correctly annotated method stubs for these parameters, use Handlers tab of the Component Inspector tool window in Studio.
-
afterActionPerformedHandler
- a handler that is invoked after selected entities are removed. It accepts the event object that can be used to get the entities selected for removal. For example:@Install(to = "customersTable.remove", subject = "afterActionPerformedHandler") protected void customersTableRemoveAfterActionPerformedHandler(RemoveOperation.AfterActionPerformedEvent<Customer> event) { System.out.println("Removed " + event.getItems()); }
-
actionCancelledHandler
- a handler that is invoked when the remove operation is cancelled by user in the confirmation dialog. It accepts the event object that can be used to get the entities selected for removal. For example:@Install(to = "customersTable.remove", subject = "actionCancelledHandler") protected void customersTableRemoveActionCancelledHandler(RemoveOperation.ActionCancelledEvent<Customer> event) { System.out.println("Cancelled"); }
If you want to perform some checks or interact with the user in a special way before the action is executed, subscribe to the action’s ActionPerformedEvent
and invoke execute()
method of the action when needed. The action will be invoked with all parameters that you defined for it. In the example below, we show a custom confirmation dialog before executing the action:
@Named("customersTable.remove")
private RemoveAction customersTableRemove;
@Subscribe("customersTable.remove")
public void onCustomersTableRemove(Action.ActionPerformedEvent event) {
customersTableRemove.setConfirmation(false);
dialogs.createOptionDialog()
.withCaption("My fancy confirm dialog")
.withMessage("Do you really want to remove the customer?")
.withActions(
new DialogAction(DialogAction.Type.YES)
.withHandler(e -> customersTableRemove.execute()), // execute action
new DialogAction(DialogAction.Type.NO)
)
.show();
}
You can also subscribe to ActionPerformedEvent
and instead of invoking the action’s execute()
method, use RemoveOperation
API directly to remove the selected entities. In this case, you are ignoring all specific action parameters and behavior and using only its common parameters like caption, icon, etc. For example:
@Inject
private RemoveOperation removeOperation;
@Subscribe("customersTable.remove")
public void onCustomersTableRemove(Action.ActionPerformedEvent event) {
removeOperation.builder(customersTable)
.withConfirmationTitle("Removing customer...")
.withConfirmationMessage("Do you really want to remove the customer?")
.remove();
}