Render a component in grid using PropertyValueGenerator?

Hi,

In a grid, I would like to render different components in rows based on a flag, but instead I get only the toStirng representation of the components in the UI. How can this be achieved in a grid?

Following is the snippet I tried

BeanItemContainer<Sample> beanContainer = new BeanItemContainer<Sample>(Sample.class, sampleList);
GeneratedPropertyContainer container = new GeneratedPropertyContainer(beanContainer);
container.addGeneratedProperty("columnDisplayed", new PropertyValueGenerator<Component>() {
  @Override
   public Component getValue(Item item, Object itemId, Object propertyId) {
      if(((Sample)itemId).isFlag) {
         return new Button("Foo");
      }
      return new Label("Bar");
   } 

   @Override
   public Class<Component> getType() {
      return Component.class;
   }
});
Grid grid = new Grid(container);

Officially not (yet) supported. You could use this unofficial add-on from Directory. It has a ComponentGrid<> component.

https://vaadin.com/directory#!addon/componentrenderer

Hi!

Thanks for the response.
One more thing, is there another way to achieve this without using an addon?

Well you can use grid.getColumn(yourPropertyId).setRenderer(new ButtonRenderer(…

But that always puts a NativeButton to the column. You can’t set it to alternatively output a button or a text value.

Hi!

Could you explain how the rows can alternatively display button and text values?
And I also want to show a combobox in some particular rows based on the condition.
Is there a specific renderer available for Combobox?

Thanks

[i]

Could you explain how the rows can alternatively display button and text values?
[/i]
You need a custom Renderer

[i]

Is there a specific renderer available for Combobox?
[/i]
No such Renderer available in Core Framework.

I recommend you trying out the component renderer add-on. Makes your life easier than trying to implement Renderer yourself.

It was easily possible in Table using the columnGenerator but we are asked to use Grid only.
Wish Grid had this feature too.

Anyways, many thanks for your time :slight_smile:

I share your frustration. I hope that this feature is the next in line to be implemented in Grid.