Search code examples
vaadin7vaadin-grid

How to hide the button in generated column in grid?


I have grid with the button in the generated column. See screenshot:

enter image description here

Depending on boolean value in the column "Has Stacktrace" I would like to show or hide the button "view" in the column "Stacktrace".

Should I extend Vaadin's com.vaadin.ui.renderers.ButtonRenderer or there is any simpler option?


Solution

  • I did the following workaround, but I still would like to know how to make a button:

    // create container
    IndexedContainer container = new IndexedContainer(...);
    container.addContainerProperty("stacktrace", String.class, null);
    
    // set data
    importSession.getRows().forEach(importSessionRow -> {
        Item item = gpcontainer.getItem(importSessionRow.getId());
        item.getItemProperty("stacktrace").setValue(importSessionRow.isHasStacktrace() ? "/admin/instrument/import/row/" + importSessionRow.getId() + "/stacktrace.html" : null);
    });
    
    // set renderer
    grid.getColumn("stacktrace").setRenderer(new HtmlRenderer(),
                new Converter<String, String>() {
                    @Override
                    public String convertToModel(String value,
                                                 Class<? extends String> targetType, Locale locale)
                            throws Converter.ConversionException {
                        return "not implemented";
                    }
    
                    @Override
                    public String convertToPresentation(String value,
                                                        Class<? extends String> targetType, Locale locale)
                            throws Converter.ConversionException {
                        return value != null ? "<a href='" + value + "' target='_blank'>view</a>" : null;
                    }
    
                    @Override
                    public Class<String> getModelType() {
                        return String.class;
                    }
    
                    @Override
                    public Class<String> getPresentationType() {
                        return String.class;
                    }
                });