09_Grid_Manuell
Code-Dateien
| Dateiname | Aktion |
|---|---|
| CODECode_Bank.zip | Download |
| CODECode_Pizza.zip | Download |
| CODECode_Urlaub.zip | Download |
Videos
| Dateiname | Aktion |
|---|---|
| VIDEOVideo_Bank_D | Abspielen |
| VIDEOVideo_Pizza_E | Abspielen |
| VIDEOVideo_Urlaub_D | Abspielen |
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);
grid.addColumn(o -> o.getOrderId())
- Adds a new column to the Grid
o= onePizzaOrderobject (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());
addComponentColumn(pizzaOrder -> { ... })
- Adds a column that displays components (Checkbox)
pizzaOrder= one row (onePizzaOrderobject)
π 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= onePizzaOrdero.getGarlic()returnstrueorfalse
π Sorting result:
falsefirsttrueafter
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
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);
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= onePizzaOrder
π 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")
- πΌ image (
π Header looks like:
[π image] Size
Enable sorting
.setSortable(true);
- User can click the header
- Grid sorts by
size