Search code examples
vaadinvaadin-gridvaadin23

Grid Row Freezing


Is there a way to freeze the top-most row in a Vaadin Grid so that it can always be viewed, no matter how much the user scrolls vertically? I was looking at https://vaadin.com/docs/latest/components/grid/#column-freezing, but I'm not sure if this would help for rows.


Solution

  • Based on idea given in the commments, here is a proof of concept code that demonstrate how to do it Vaadin Grid.

        Grid<String> grid = new Grid<>();
        grid.setHeight("250px");
        grid.setWidth("250px");
        GridListDataView<String> dataView = grid.setItems("Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten");
        Column<String> col = grid.addColumn(value -> value.toString()).setHeader("A number");
        HeaderRow row = grid.appendHeaderRow();
        Div div = new Div();
        div.setText(dataView.getItem(0).toString());
        div.getElement().getStyle().set("text-align", "left");
        div.getElement().getStyle().set("width", "100%");
        row.getCell(col).setComponent(div);                
        grid.addSelectionListener(event -> {
            event.getFirstSelectedItem().ifPresent(item -> {
                div.setText(item.toString());                
            });
        });