Teodor7
(Teodor Iancu)
August 18, 2019, 3:26pm
1
I’m trying to iterate over a HashMap to display the content in Vaadin14 Grid to look like this:
that’s the java class:
public class MyClass extends VerticalLayout {
// Dummy Data
LocalDate date1 = LocalDate.now();
LocalDate date2 = LocalDate.now();
LocalDate date3 = LocalDate.now();
Boolean[] isPresent1 = {true, false, false, true, true, false, false, true, true};
Boolean[] isPresent2 = {true, false, false, true, true, false, false, true, true};
Boolean[] isPresent3 = {true, false, false, true, true, false, false, true, true};
Map<LocalDate, Boolean[]> trainingsMap = new HashMap<>();
Grid<Map.Entry<LocalDate, Boolean[]>> grid = new Grid<>();
Icon icon;
public MyClass() {
// in reality data come from Spring Data - Repository - Service
trainingsMap.put(date1, isPresent1);
trainingsMap.put(date2, isPresent2);
trainingsMap.put(date3, isPresent3);
for (Map.Entry<LocalDate, Boolean[]> map : trainingsMap.entrySet()) {
grid.addColumn(new ComponentRenderer<Component, Map<LocalDate, Boolean[]>>(createIsPresent(map.getValue())));
// .setHeaderRenderer(new LocalDateTimeRenderer<>(map.getKey(), "dd/MM HH:mm:ss"))); //
}
grid.setItems(trainingsMap.entrySet());
add(grid);
}
private Component createIsPresent(Boolean[] isPresent) {
for (Boolean b : isPresent) {
if (b) {
icon = UIUtils.createPrimaryIcon(VaadinIcon.CHECK);
}
}
return icon;
}
}
Error: Cannot resolve constructor com.vaadin.flow.data.renderer.ComponentRenderer<>
Is this possible at all or am I doing something fundamentally wrong?
Stuart
(Stuart Robinson)
August 18, 2019, 8:13pm
2
Have you tried it with the arguments Component, Map<LocalDate, Boolean > ?
i.e.
for (Map.Entry<LocalDate, Boolean[]> map : trainingsMap.entrySet()) {
grid.addColumn(new ComponentRenderer<Comnponent, Map<LocalDate, Boolean[]>>(createIsPresent(map.getValue()))
.setHeaderRenderer(new LocalDateTimeRenderer<>(map.getKey(), "dd/MM HH:mm:ss")));
}
S.
Teodor7
(Teodor Iancu)
August 18, 2019, 9:36pm
3
Thanks Stuart,
I make some changes in code (including your recommendation) but now this error:
Error: Cannot resolve constructor com.vaadin.flow.data.renderer.ComponentRenderer<>
Teodor7
(Teodor Iancu)
August 18, 2019, 9:53pm
5
Stuart Robinson:
post you code
package com.ui.views.menu;
import com.ui.MainLayout;
import com.ui.util.UIUtils;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.data.renderer.LocalDateTimeRenderer;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
@Route(value = "erwachseneListe", layout = MainLayout.class)
@PageTitle("erwachseneListe")
//@Secured(Role.ADMIN)
public class ErwachseneListe extends VerticalLayout {
// Dummy Data
LocalDate date1 = LocalDate.now();
LocalDate date2 = LocalDate.now();
LocalDate date3 = LocalDate.now();
Boolean[] isPresent1 = {true, false, false, true, true, false, false, true, true};
Boolean[] isPresent2 = {true, false, false, true, true, false, false, true, true};
Boolean[] isPresent3 = {true, false, false, true, true, false, false, true, true};
Map<LocalDate, Boolean[]> trainingsMap = new HashMap<>();
Grid<Map.Entry<LocalDate, Boolean[]>> grid = new Grid<>();
Icon icon;
public ErwachseneListe() {
// in reality data come from Spring Data - Repository - Service
trainingsMap.put(date1, isPresent1);
trainingsMap.put(date2, isPresent2);
trainingsMap.put(date3, isPresent3);
for (Map.Entry<LocalDate, Boolean[]> map : trainingsMap.entrySet()) {
grid.addColumn(new ComponentRenderer<Component, Map<LocalDate, Boolean[]>>(createIsPresent(map.getValue())));
// .setHeaderRenderer(new LocalDateTimeRenderer<>(map.getKey(), "dd/MM HH:mm:ss"))); //
}
grid.setItems(trainingsMap.entrySet());
add(grid);
}
private Component createIsPresent(Boolean[] isPresent) {
for (Boolean b : isPresent) {
if (b) {
icon = UIUtils.createPrimaryIcon(VaadinIcon.CHECK);
}
}
return icon;
}
}
Stuart
(Stuart Robinson)
August 18, 2019, 10:57pm
6
Ok, it needs to look like this…
for (Map.Entry<LocalDate, Boolean[]> map : trainingsMap.entrySet()) {
grid1.addColumn(new ComponentRenderer<Component, Map.Entry<LocalDate, Boolean[]>>(entry -> createIsPresent(entry.getValue())));
// .setHeaderRenderer(new LocalDateTimeRenderer<>(map.getKey(), "dd/MM HH:mm:ss"))); //
}
Stuart
Teodor7
(Teodor Iancu)
August 19, 2019, 3:08pm
7
Thanks Stuart, maybe is possible to put a HashMap into a non Bean grid but I think is not possible to inserted also the “Person” in the first column, so I will try another way (Polymer 3) to do this.
Stuart
(Stuart Robinson)
August 19, 2019, 4:14pm
8
You could use a ValueProvider and pass the ‘person’ object and return person.getUsername();
Stuart.
Teodor7
(Teodor Iancu)
August 19, 2019, 5:13pm
9
try and you will see what is not possible to have Grid<Person>
and Grid<Map.Entry<LocalDate, Boolean[]>>
in the same view.
Stuart
(Stuart Robinson)
August 19, 2019, 7:46pm
10
Could you structure your data like this…
Map.Entry<Person, Map.Entry<LocalDate, Boolean[]>>>
Grid<Map.Entry<Person, Map.Entry<LocalDate, Boolean[]>>> grid = new Grid<>();
Stuart
Teodor7
(Teodor Iancu)
August 19, 2019, 7:55pm
11
I try but it doesn’t work to show the data like above.
Did you try to execute the cod?