Enable and disable edit action on entity selected

Dear CUBA platform support team,
Is there any way to Enable and Disable edit action on entity selected in function of some entity’s parameter?

Hi Francesco,

In CUBA before 6.2 you can override isApplicable method of EditAction:


@Inject
private Table<Client> clientsTable;
...
@Override
public void init(Map<String, Object> params) {
    super.init(params);
    
    clientsTable.addAction(new EditAction(clientsTable) {
        @Override
        protected boolean isApplicable() {
            Client client = clientsTable.getSingleSelected();
            if (client != null && client.getName().startsWith("a")) {
                return false;
            }
            return super.isApplicable();
        }
    });
}

If you use CUBA 6.2 you can add enabled rule for EditAction:


@Named("clientsTable.edit")
private EditAction clientsTableEdit;
@Inject
private Table<Client> clientsTable;
...
@Override
public void init(Map<String, Object> params) {
    super.init(params);

    clientsTableEdit.addEnabledRule(() -> {
        Client client = clientsTable.getSingleSelected();
        return client != null && !client.getName().startsWith("a");
    });
}

Also you have another option to react on a selection change of a Table (or any other event/listener), just use ItemChangeListener for a CollectionDatasource and change enabled property of an Action:


@Named("clientsTable.edit")
private EditAction clientsTableEdit;
@Inject
private Table<Client> clientsTable;
@Inject
private CollectionDatasource<Client, UUID> clientsDs;
...
@Override
public void init(Map<String, Object> params) {
    super.init(params);
    
    clientsDs.addItemChangeListener(e -> {
        Client client = clientsTable.getSingleSelected();
        if (client != null && client.getName().startsWith("a")) {
            clientsTableEdit.setEnabled(false);
        } else {
            clientsTableEdit.setEnabled(true);
        }
    });
}
1 Like

Thanks

Hello,

I’m trying to do something similar.
I have two tables, and I want to enable or disable the Add and Remove button of the second table (named inlineParamsTable) depening on selection of first table (named integrationPointsTable).

I adapted the example code of third option (I’m using cuba 6.3) but when I open the window of my app, it throws me the following exception:

“IllegalArgumentException: Can not set com.haulmont.cuba.gui.components.actions.RemoveAction field com.company.rtdtestapp.web.inlineservice.InlineServiceEdit.inlineParamsTableRemove to com.haulmont.cuba.gui.xml.DeclarativeAction”

Can you help me please?

Code blocks


 @Inject
    private CollectionDatasource<InlineParams, UUID> inlineParamsDs;

    @Inject
    private CollectionDatasource<InlineIntegrationPoints, UUID> integrationPointsDs;

    @Inject
    private Metadata metadata;

    @Named("inlineParamsTable.add")
    private RemoveAction inlineParamsTableAdd;
    
    @Named("inlineParamsTable.remove")
    private RemoveAction inlineParamsTableRemove;
    
    @Inject
    private Table<InlineIntegrationPoints> integrationPointsTable;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);

        integrationPointsDs.addItemChangeListener(e -> {
            InlineIntegrationPoints client = integrationPointsTable.getSingleSelected();
            if (client != null) {
                inlineParamsTableAdd.setEnabled(true);
                inlineParamsTableRemove.setEnabled(true);
            } else {
                inlineParamsTableAdd.setEnabled(false);
                inlineParamsTableRemove.setEnabled(false);
            }
        });
    }

    public void addIntegrationParam() {
        InlineParams inlineParam = metadata.create(InlineParams.class);
        inlineParam.setInlineIntegrationPoints(integrationPointsDs.getItem());
        inlineParamsDs.addItem(inlineParam);
    }

    public void removeIntegrationParam() {
        if (inlineParamsDs.getItem() != null) {
            inlineParamsDs.removeItem(inlineParamsDs.getItem());
        }
    }

Probably your “Remove” action is not actually a standard com.haulmont.cuba.gui.components.actions.RemoveAction but a custom action invoking the removeIntegrationParam method - hence the error on assignment.

Try to declare this action as com.haulmont.cuba.gui.components.Action.