18

With Vaadin Grid, I want to generate multiline cells for every cell that have more content in cell that overlaps its width.

I have allready tried:

  • java \n new line character and CSS stylings like white-space: pre; but it does not seem to work. (This solution worked for Table)

  • custom renderer setRenderer(HtmlRenderer) with </br>tags and different CSS display settings

Desired result: Wanted result, implemented with Vaadin Table

Zigac
  • 1,551
  • 1
  • 16
  • 27
  • UPDATE: Vaadins Grid 2 comes with 'Variable row height support' https://vaadin.com/blog/-/blogs/-vaadin-grid-2-0-with-million-dollar-scrolling There is no Java API supporting it yet, but there might be alternative: "With Vaadin Framework 8.0, we included an enabling piece of technology called HTML imports. This allows WebComponents such as to be used together with Framework 8.0." – Zigac Apr 24 '17 at 17:53

5 Answers5

3

Vaadin Grid still doesn't have native support for multiline columns but you can try some improvisation, on example I suggest to see the Vaadin forum discussion on the link: Grid - How to display multiple lines in one cell

Also you can try to create custom renderer for the Grid.

Dragan Radevic
  • 791
  • 7
  • 17
  • Thank you for your anwser Dragan. Could you provide some sample implementation? – Zigac Mar 24 '16 at 14:17
  • See the link https://vaadin.com/docs/-/part/framework/components/components-grid.html#components.grid.renderer I suggest to try implement HtmlRenderer – Dragan Radevic Mar 24 '16 at 14:29
  • 1
    I have done that but, row is not vertically streched, so no content is shown after first
    .
    – Zigac Mar 24 '16 at 15:23
  • 1
    For the first it seems like a Vaadin bug. I think that you can note as bug on https://vaadin.com/bug . Secondly, you can use: https://vaadin.com/directory#!addon/componentrenderer add on and place a VerticalLayout with labels in column of you grid. It is easier than creating your own renderer. But creating your own renderer is more performant I think. – Dragan Radevic Mar 28 '16 at 12:05
  • Thank you Dragan for provided workaround. But i'm looking for native implementation. – Zigac Mar 29 '16 at 21:14
3

For people with large text in columns and who want to display large data in Grid Columns without having them cutoff by default:

  1. Add a Style name to your grid:

    grid.addStyleName("commentsGrid");

  2. Add some style names to your rows and cells if you would like to customize them:

    grid.setRowStyleGenerator(rowRef -> {// Java 8
        return "bigRows";
    }); 
    

    and

    grid.setCellStyleGenerator(new Grid.CellStyleGenerator(){
         @Override
         public String getStyle(CellReference cellReference) {
             return "bigCell";
         }
    });
    
  3. For me the problem column was comments. So surround the text with p tag with style class wrap and a fixed width.

    Converter<String, String> commentsConverter = new Converter<String,  String>(){
            @Override
            public String convertToModel(String value, Class<? extends String> targetType, Locale locale)
                    throws com.vaadin.data.util.converter.Converter.ConversionException {
    
                return value;
            }
    
            @Override
            public String convertToPresentation(String value, Class<? extends String> targetType, Locale locale)
                    throws com.vaadin.data.util.converter.Converter.ConversionException {
                if(value !=null){
                    return "<p class=\"wrap\">"+value+"</p>";
                }else{
                    return "";
                }
            }
    
            @Override
            public Class<String> getModelType() {
                return String.class;
            }
    
            @Override
            public Class<String> getPresentationType() {
                return String.class;
            }
    
        };
        grid.getColumn("comments").setRenderer(new HtmlRenderer(), commentsConverter);
        grid.getColumn("comments").setWidth(700d);
    
  4. Then i styled the classes mentioned above as follows:

    .commentsGrid td{
        height:150px !important;
     }
    
    p.wrap{
      width: 45em;
      word-wrap: break-word;
      word-break: break-all;
      white-space: normal; 
    }
    

And i got result like this:

I am not a CSS ninja, so you can make it way more beautiful than this.

enter image description here

If you don't like all rows to have equally large heights then you can calculate rowHeights on the fly and then set them in step 2. I am not 100% sure.

3

Another way of fitting long scrolling text into a grid column is to set a row height for the grid and use a component renderer and render a textarea in the grid which contains the text. This works good because if the text is too long the user will get a scrollbar inside the cell to see the overflowed text.

Column screen = grid.addComponentColumn(role->{
        TextArea ta = new TextArea();
        ta.setValue(VALUE_PROVIDER_TEXT);
        ta.setStyleName("clear");
        ta.setHeight("100px");
        ta.setReadOnly(true);
        ta.setWidth("150px");
        return ta;
    });
    screen.setWidth(150);
    screen.setCaption("Screens");
    grid.setBodyRowHeight(100);

and this in the css

  .v-textarea-clear{background-color: transparent;border: none;}

Example

mgrecol
  • 41
  • 1
0

Have you tried to add a style renderer to your ceolumns to add a custom css class, where you can place your defined height?

Example:

Java:

grid.setCellStyleGenerator(cellRef -> // Java 8
    "heigher");

CSS:

.v-grid-cell.heigher {
    height: 10em;
}

Example coming from: https://vaadin.com/docs/-/part/framework/components/components-grid.html

Rene M.
  • 2,660
  • 15
  • 24
0

With Vaadin 24 we can use a theme variant for this:

grid.addThemeVariants(GridVariant.LUMO_WRAP_CELL_CONTENT);

Other grid variants are described in the official docs.

rbs
  • 1,087
  • 2
  • 17
  • 24