我使用Vaadin创建一个包含单个TreeGrid的简单Web应用程序。 虽然显示了TreeGrid,但是尽管有子级,但没有用于展开或折叠根元素的句柄(“ 2010年”或“ 2011年”)。 当我使用expand(..)方法时,树会展开(如屏幕截图所示),但是没有图标可以将其折叠。在“ 2011年”下面的屏幕截图中,是展开的根节点,其中“客户项目1”和“客户项目2”是子级。 该项目是使用Maven构建的,我使用vaadin-bom版本13.0.2和vaadin-grid-flow-3.0.3.jar。 屏幕截图下方是我使用的代码。我认为问题一定在方法createTreeGrid()中。 Vaadin的工作示例可以在这里找到 Vaadin example
那么有人对如何解决这个问题有任何想法吗?任何帮助表示赞赏... :-)
战栗
package hello;
@Route
public class MainView extends VerticalLayout
{
public MainView()
{
add( createTreeGrid() );
}
private TreeGrid<Project> createTreeGrid()
{
TreeGrid<Project> treeGrid = new TreeGrid<>();
final List<Project> generateProjectsForYears = generateProjectsForYears( 2010, 2016 );
treeGrid.setItems( generateProjectsForYears, Project::getSubProjects );
treeGrid.addColumn( Project::getName ).setHeader( "Project Name" ).setId( "name-column" );
treeGrid.addColumn( Project::getHoursDone ).setHeader( "Hours Done" );
treeGrid.addColumn( Project::getLastModified ).setHeader( "Last Modified" );
treeGrid.expand( generateProjectsForYears.get( 1 ) ); // works!
return treeGrid;
}
private List<Project> generateProjectsForYears( int startYear, int endYear )
{
List<Project> projects = new ArrayList<>();
for ( int year = startYear; year <= endYear; year++ )
{
Project yearProject = new Project( "Year " + year );
Random random = new Random();
for ( int i = 1; i < 2 + random.nextInt( 5 ); i++ )
{
Project customerProject = new Project( "Customer Project " + i );
customerProject.setSubProjects( Arrays.asList(
new LeafProject( "Implementation", random.nextInt( 100 ), year ),
new LeafProject( "Planning", random.nextInt( 10 ), year ),
new LeafProject( "Prototyping", random.nextInt( 20 ), year ) ) );
yearProject.addSubProject( customerProject );
}
projects.add( yearProject );
}
return projects;
}
private class Project
{
private List<Project> subProjects = new ArrayList<>();
private String name;
public Project( String name )
{
this.name = name;
}
public String getName()
{
return name;
}
public List<Project> getSubProjects()
{
return subProjects;
}
public void setSubProjects( List<Project> subProjects )
{
this.subProjects = subProjects;
}
public void addSubProject( Project subProject )
{
subProjects.add( subProject );
}
public int getHoursDone()
{
return getSubProjects().stream()
.map( project -> project.getHoursDone() )
.reduce( 0, Integer::sum );
}
public Date getLastModified()
{
return getSubProjects().stream()
.map( project -> project.getLastModified() )
.max( Date::compareTo ).orElse( null );
}
}
private class LeafProject extends Project
{
private int hoursDone;
private Date lastModified;
public LeafProject( String name, int hoursDone, int year )
{
super( name );
this.hoursDone = hoursDone;
Random random = new Random();
lastModified = new Date( year - 1900, random.nextInt( 12 ),
random.nextInt( 10 ) );
}
@Override
public int getHoursDone()
{
return hoursDone;
}
@Override
public Date getLastModified()
{
return lastModified;
}
}
}
答案 0 :(得分:2)
您必须定义一个层次结构列。
因此只需替换
treeGrid.addColumn( Project::getName ).setHeader( "Project Name" ).setId( "name-column" );
与
treeGrid.addHierarchyColumn( Project::getName ).setHeader( "Project Name" ).setId( "name-column" );