09_Grid_Manuell

Code-Dateien

DateinameAktion
CODECode_Bank.zipDownload
CODECode_Pizza.zipDownload
CODECode_Urlaub.zipDownload

Videos

DateinameAktion
VIDEOVideo_Bank_DAbspielen
VIDEOVideo_Pizza_EAbspielen
VIDEOVideo_Urlaub_DAbspielen

Lernmaterialien

No automatic columns

autoCreateColumn = false

    private final Grid<Urlaub> grid = new Grid<>(Urlaub.class, false);

autoCreateColumn = false

  • Means: NO automatic columns

Vaadin will NOT create columns like:

  • orderId
  • pizza
  • price
  • etc.

add simple columns

        grid.addColumn(o -> o.getOrderId())
                .setHeader("Order ID")
                .setSortable(true);
        grid.addColumn(o -> o.getOrderDate())
                .setHeader("Order Date")
                .setSortable(true);
        grid.addColumn(o -> o.getPizza())
                .setHeader("Pizza")
                .setSortable(true);
        grid.addColumn(o -> o.getSize())
                .setHeader("Size")
                .setSortable(true);
        grid.addColumn(o -> o.getQuantity())
                .setHeader("Quantity")
                .setSortable(true);
        grid.addColumn(o -> {return o.getGarlic() ? "Y" : "N";})
                .setHeader("Garlic")
                .setSortable(true);
001.png

grid.addColumn(o -> o.getOrderId())

  • Adds a new column to the Grid
  • o = one PizzaOrder object (one row)

πŸ‘‰ For each row:

o.getOrderId() is called β†’ value is shown in the cell

βœ… Equivalent to:

grid.addColumn(PizzaOrder::getOrderId);

.setHeader("Order ID")

  • Sets the column title (header text)

πŸ‘‰ The column will display:

Order ID instead of something like orderId

.setSortable(true)

  • Enables sorting on this column

πŸ‘‰ User can click the column header:

  • first click β†’ ascending
  • second click β†’ descending

add CheckBox

        grid.addComponentColumn(pizzaOrder -> {
            Checkbox cb = new Checkbox(pizzaOrder.getGarlic());
            cb.setReadOnly(true);
            return cb;
        })
                .setHeader("Garlic")
                .setSortable(true)
                .setComparator(o -> o.getGarlic());
002.png

addComponentColumn(pizzaOrder -> { ... })

  • Adds a column that displays components (Checkbox)
  • pizzaOrder = one row (one PizzaOrder object)

πŸ‘‰ This lambda replaces the old ValueProvider

Create the Checkbox

Checkbox cb = new Checkbox(pizzaOrder.getGarlic());

  • Reads garlic (true/false)
  • Creates a checkbox with that value

πŸ‘‰ Result:

  • true β†’ β˜‘
  • false β†’ ☐

cb.setReadOnly(true);

  • Makes the checkbox non-editable
  • User cannot change it

πŸ‘‰ It’s only for display

return cb;

  • Returns the component
  • Vaadin places it into the grid cell

.setHeader("Garlic")

  • Sets column title

πŸ‘‰ Column name = Garlic

.setSortable(true)

  • Enables sorting when clicking the column header

πŸ‘‰ BUT:

  • Grid doesn’t know how to sort a Checkbox

.setComparator(o -> o.getGarlic())

This is the important part πŸ‘‡

πŸ‘‰ Tells Vaadin how to sort the rows

  • o = one PizzaOrder
  • o.getGarlic() returns true or false

πŸ‘‰ Sorting result:

  • false first
  • true after

Why comparator is needed

Without it:

  • ❌ Sorting might not work correctly
  • ❌ Vaadin cannot compare Checkbox components

With it:

  • βœ… Sorting is based on the actual data (garlic)

add header incl image

003.png
        Image p = new Image("icons/pizza.png", "Pizza");
        p.setWidth("64px");
        grid.addColumn(o -> o.getSize())
                .setHeader(new HorizontalLayout(p, new Span("Size")))
                .setSortable(true);
004.png

Create an image

Image p = new Image(β€œicons/pizza.png”, β€œPizza”);

  • Loads an image from:

    frontend/icons/pizza.png

  • "Pizza" = alternative text (for accessibility)

Set image size

p.setWidth(β€œ64px”);

  • Image width = 64 pixels

Add a column

grid.addColumn(o -> o.getSize())

  • Adds a column to the Grid
  • o = one PizzaOrder

πŸ‘‰ Shows:

o.getSize()

Example values:

  • Small
  • Medium
  • Large

Custom header

.setHeader(new HorizontalLayout(p, new Span(β€œSize”)))

  • Instead of plain text β†’ uses a layout
  • Combines:
    • πŸ–Ό image (p)
    • πŸ“ text ("Size")

πŸ‘‰ Header looks like:

[πŸ• image] Size

Enable sorting

.setSortable(true);

  • User can click the header
  • Grid sorts by size

GitHub

Copy Code