Programmatically Show/Hide Component in a Grid Cell in Vaadin 12

Hi,

I implemented the code in the “Open details programmatically” Java example on https://vaadin.com/components/vaadin-grid/java-examples/item-details

What I want to do though is to show/hide the details button in the cells based on values in the backing data object.

In Vaadin 8, I was able to do it by following https://vaadin.com/docs/v8/framework/components/components-grid.html#components.grid.stylegeneration.cell

I don’t see that as an option in Vaadin 12. What am I missing?

Thanks,
Steve

Hi Steven

Instead of using a NativeButtonRenderer, you could use a ComponentRenderer, where you can simply not create/add the detail button if certain conditions of the item are not met.

grid.addColumn(new ComponentRenderer<>(item -> {
    if(item.customCriteriaIsMet()){
	    return new NativeButton("Details", click -> grid.setDetailsVisible(item, !grid.isDetailsVisible(item)));
	}
	return null;
}));

Hi Steven,

I set the setDetailsVisibleOnClick to false and use my own click listener to toggle whether the details are shown or not.

That way I can ‘see if there are any details to show’ before calling setDetailsVisible.

grid.setDetailsVisibleOnClick(false);
grid.addItemClickListener(new ComponentEventListener<ItemClickEvent<MyObject>> () {	
			@Override
			public void onComponentEvent(ItemClickEvent<MyObject> event) {
			if (event.getItem() != null) {
					MyObject toShow = event.getItem();
					if (toShow.hasSomethingToShow()) {
						grid.setDetailsVisible(toShow, !grid.isDetailsVisible(toShow)); //Switches between show and hide.
					}
			}
		});

Hope that helps :slight_smile:

Stuart