Limit number of records in a table

Hi there,

I’m wondering if there’s a way to limit the number of records that can be created in a table. What i want to do is this: Say i limit the number of records to 10. When the 10th record is created i want to show a notification, at that same moment i want to disable the create button, so no record can be created. Of course when a record or records are deleted, the create button must be enabled. I’m using legacy screens in cuba 7.

Is the possible?

Regards,

LS

Hello!

The table is bounded with collection datasource, so you should track changes (add, remove, refresh) there. For instance:

@Named("ordersTable.create")
private CreateAction ordersTableCreate;
@Inject
private GroupDatasource<Order, UUID> ordersDs;

@Override
protected void init(InitEvent initEvent) {
    ordersDs.addCollectionChangeListener(e -> {
        boolean createEnabled = ordersDs.getItems().size() < 10;
        ordersTableCreate.setEnabled(createEnabled);
        if (!createEnabled) {
            showNotification("Records limit is 10");
        }
    });
}

Thanks Roman, this works perfect.