List view enable "editorActionsColumn" programmatically

Hi everyone is there a way to enable programmatically the editorActionsColumn instead of defining it in the xml ? Thanks

Hello!

In DataGrid editorActionsColumn is just a column with a custom renderer. You can implement the same renderer in your project and then add column programmatically.

For instance:

EditorActionsRenderer.java
public class EditorActionsRenderer<E> extends ComponentRenderer<Component, E> {

    protected Metadata metadata;
    protected AccessManager accessManager;

    protected ApplicationContext applicationContext;

    protected DataGrid<E> dataGrid;

    public EditorActionsRenderer(ApplicationContext applicationContext, DataGrid<E> dataGrid) {
        // Stub
        super(() -> new Span());

        this.applicationContext = applicationContext;
        this.metadata = applicationContext.getBean(Metadata.class);
        this.accessManager = applicationContext.getBean(AccessManager.class);

        this.dataGrid = dataGrid;
    }

    @Override
    public Component createComponent(E item) {
        MetaClass metaClass = metadata.getClass(item.getClass());

        UiEntityContext entityContext = new UiEntityContext(metaClass);
        accessManager.applyRegisteredConstraints(entityContext);

        InMemoryCrudEntityContext inMemoryContext = new InMemoryCrudEntityContext(metaClass, applicationContext);
        accessManager.applyRegisteredConstraints(inMemoryContext);

        if (entityContext.isEditPermitted()
                && (inMemoryContext.updatePredicate() == null
                || item != null && inMemoryContext.isUpdatePermitted(item))) {
            Button editButton = new Button();
            editButton.setIcon(VaadinIcon.EDIT.create());

            editButton.addClickListener(__ -> {
                if (dataGrid.getEditor().isOpen()) {
                    dataGrid.getEditor().cancel();
                }
                dataGrid.getEditor().editItem(item);
            });
            return editButton;
        }

        return new Span();
    }
}

Then in a view add a column:

@ViewComponent
private DataGrid<User> usersDataGrid;

@Subscribe
public void onInit(final InitEvent event) {
    DataGridColumn<User> editorActionsColumn = usersDataGrid.addColumn(new EditorActionsRenderer<>(getApplicationContext(), usersDataGrid));
    editorActionsColumn.setEditorComponent(createEditorActions());
}

protected HorizontalLayout createEditorActions() {
    HorizontalLayout actions = new HorizontalLayout();
    actions.setPadding(false);

    Button saveButton = new Button("Save");
    saveButton.addClickListener(__ -> usersDataGrid.getEditor().save());
    actions.add(saveButton);

    Button cancelButton = new Button("Cancel");
    cancelButton.addClickListener(__ -> usersDataGrid.getEditor().cancel());
    actions.add(cancelButton);

    return actions;
}

For more details about editor renderer, take a look at how it is loading from XML in AbstractGridLoader#loadEditorActionsColumn().

1 Like