Hi everyone. Is it possible to search in TreeGrid? Not only for the parent entries, but also for the child entries. I want to be able to search for the name of child entries. And on text field change to filter the info that i need.
This is what i have done. I created a TreeGrid and a TextField for searching. At the moment the search is working only for the parent entries.
I am using Vaadin 8.4.3
TreeGrid
Name Type
2017 Projects 100
-Project1 20
-Project2 30
-Project3 50
2018 Projects 50
-Project4 30
-Project5 20
If i search for 2017 Projects - should display
2017 Projects 100
-Project1 20
-Project2 30
-Project3 50
If i search for for Project1 - should display
2017 Projects 100
-Project1 20
or just
-Project1 20
TreeGrid POJO:
public class TreeGridDTO {
private String name;
private String type;
private List<TreeGridDTO> internalList;
public TreeGridDTO(String name, String type){
this.name = name;
this.type = type;
this.internalList = new ArrayList<>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void addToInternalList(TreeGridDTO treeGridDTO){
this.internalList.add(treeGridDTO);
}
public List<TreeGridDTO> getInternalList() {
return internalList;
}
}
//creating the TreeGrid
TreeGridDTO treeGridDTO1 = new TreeGridDTO("2017 Projects","100");
TreeGridDTO treeGridDTO2 = new TreeGridDTO("Project1","20");
TreeGridDTO treeGridDTO3 = new TreeGridDTO("Project2","30");
TreeGridDTO treeGridDTO4 = new TreeGridDTO("Project3","50");
treeGridDTO1.addToInternalList(treeGridDTO2);
treeGridDTO1.addToInternalList(treeGridDTO3);
treeGridDTO1.addToInternalList(treeGridDTO4);
TreeGridDTO treeGridDTO5 = new TreeGridDTO("2018 Projects","50");
TreeGridDTO treeGridDTO6 = new TreeGridDTO("Project4","30");
TreeGridDTO treeGridDTO7 = new TreeGridDTO("Project5","20");
treeGridDTO5.addToInternalList(treeGridDTO6);
treeGridDTO5.addToInternalList(treeGridDTO7);
List<TreeGridDTO> treeGridDTOList = new ArrayList<>();
treeGridDTOList.add(treeGridDTO1);
treeGridDTOList.add(treeGridDTO5);
TreeGrid<TreeGridDTO> grid = new TreeGrid<>();
grid.setSizeFull();
grid.setItems(treeGridDTOList, TreeGridDTO::getInternalList);
grid.addColumn(TreeGridDTO::getName).setCaption("Name");
grid.addColumn(TreeGridDTO::getType).setCaption("Type");
//search method
private List<TreeGridDTO> doSearchBasic(String searchCriteria) {
return treeGridDTOList.stream()
.filter(treeGridDTO -> treeGridDTO.getName().toLowerCase().contains(searchCriteria.toLowerCase()))
.collect(Collectors.toList());
}
// text field for search
TextField tfSearch = new TextField();
tfSearch.addValueChangeListener(valueChangeEvent -> {
grid.setItems(doSearchBasic(valueChangeEvent.getValue()), TreeGridDTO::getInternalList);
});