Update item state after custom action

I have a custom action (ItemTrackingAction) on a datagrid. After the action item changes, but then i cannot remove it unless i refresh the items in datagrid. How can i refresh the items in datagrid from the custom action.

cancel_tickets

My action execute method

@Override
    public void execute() {
        checkTarget();

        Booking booking = target.getSingleSelectedItem();
        if (booking == null) {
            throw new IllegalStateException(String.format("There is not selected item in %s target", getClass().getSimpleName()));
        }

        dialogs.createOptionDialog()
            .withHeader(messages.getMessage(CancelReservationAction.class, "cancelReservationAction.confirm.title"))
            .withText(messages.getMessage(CancelReservationAction.class, "cancelReservationAction.confirm.message"))
            .withActions(
                new DialogAction(DialogAction.Type.YES).withHandler(e -> {
                    bookingService.cancel(booking);
                }),
                new DialogAction(DialogAction.Type.NO))
            .open();
    }

Hi,

According to the error message, I assume that bookingService.cancel saves an entity which leads to optimistic blocking because CollectionContainer still stores previous item.

In your case bookingService.cancel have to return saved object and after that you need to replace an item in CollectionContainer by using itemsDc.replaceItem(...).

Regards,
Gleb

ΟΚ my bookingService.cancel now returns the saved object. How can i get CollectionContainer within my custom action so i can replace the item?

Hi,

Try something like this:

if (target.getItems() instanceof ContainerDataGridItems<User> containerItems) {
    containerItems.getContainer().replaceItem(...);
}

Gleb

1 Like

What actually worked

Booking canceledBooking = bookingService.cancel(booking);
if (target instanceof DataGrid<Booking> dg && dg.getItems() instanceof ContainerDataGridItems<Booking> items) {
    items.getContainer().replaceItem(canceledBooking);
}