Editable dataGrid behavior with criteria

I have 4 different columns in a table that is editable. However, out of 4 columns, I want to make one column editable only when the specific criteria are met.

As you see in the following code, I want the column “vatAmount” should be editable when the criteria beliw is met. All other columns (3) should remain always editable. The code below is not working, thanks for any help.

DataGridEditor<SalesPriceLine> editor = salesPriceLineTable.getEditor();
        salesPriceLineTable.addItemClickListener(e -> {
           
            if(e.getColumn().equals("vatAmount")) {
              
                if (getEditedEntity().getFixedVatPerUnit()) {
                    editor.editItem(e.getItem());
                    Component editorComponent = e.getColumn().getEditorComponent();
                    if (editorComponent instanceof Focusable) {
                        ((Focusable) editorComponent).focus();
                    }
                }

            }else{
                editor.editItem(e.getItem());
                Component editorComponent = e.getColumn().getEditorComponent();
                if (editorComponent instanceof Focusable) {
                    ((Focusable) editorComponent).focus();
                }
            }
        });

It looks like you can use a column editor component. The next example demonstrates how to disable the “email” column based on Customer lastName:

    @Subscribe
    public void onBeforeShow(final BeforeShowEvent event) {
        DataGridEditor<Customer> editor = customersDataGrid.getEditor();

        editor.setColumnEditorComponent("email", generationContext -> {
            Customer customer = generationContext.getItem();
            TypedTextField textField = uiComponents.create(TypedTextField.class);
            textField.setValueSource(generationContext.getValueSourceProvider().getValueSource("email"));
            textField.setReadOnly(!"Doe".equals(customer.getLastName()));
            return textField;
        });
    }
1 Like

Hi Maxim
Thanks for the code snippets. It’s now working after I have placed your code in onBeforeShow() while I keep my following code in onInit() event (and the respective column is editable in xml file).

 @Subscribe
    public void onInit(InitEvent event) {
//For editable columns
        DataGridEditor<SalesPriceLine> editor = salesPriceLineTable.getEditor();
        salesPriceLineTable.addItemClickListener(e -> {
            editor.editItem(e.getItem());
            Component editorComponent = e.getColumn().getEditorComponent();
            if (editorComponent instanceof Focusable) {
                ((Focusable) editorComponent).focus();
            }
        });
}