Implementing Before Action Performed Handler

Hello,

I want is such that the removeAction can only work if the paid amount =<0.

image

Now unfortunately my code below doesn’t work because as long as one of the schedule has been paid, the Datasource will aggregate the paid amounts and they will always be more than 0. How can I enforce it to limit to only the selected line?

@Inject
private CollectionDatasource<PaymentSchedule, UUID> paymentScheduleDs;
@Named("paymentScheduleTable.remove")
private RemoveAction removeAction;

@Override
public void init(Map<String, Object> params) {
    removeAction.setBeforeActionPerformedHandler(new Action.BeforeActionPerformedHandler() {
        @Override
        public boolean beforeActionPerformed() {
            return paymentScheduleDs.getItems().iterator().next().getPaidAmount() <= 0; // || paymentScheduleDs.getItems().iterator().next().getPaidAmount() == 0;
        }
    });

    super.init(params);

}

Thanks in advance.

Regards,
Kenince

Hi Kenince!

Actually, in your code the datasource doesn’t aggregate any values, you only check the value from the first row.
I’d advise you to use EnabledRule to enable and disable the action as follows:

// we can use lambda expression instead of anonymous class
removeAction.addEnabledRule(() -> {
    PaymentSchedule item = paymentScheduleDs.getItem(); // get selected item
    return selectedItem != null && selectedItem.getPaidAmount() <= 0;
});
1 Like