Vaadin TreeGrid 没有展开或折叠图标

Vaadin TreeGrid has no expand or collapse icon

我使用 Vaadin 创建了一个包含单个 TreeGrid 的简单 Web 应用程序。 显示了 TreeGrid,但没有用于展开或折叠根元素("Year 2010" 或 "Year 2011")的句柄,尽管有子元素。 当我使用 expand(..) 方法时,树被展开(如屏幕截图所示),但没有图标可以折叠它。在下面的屏幕截图中,"Year 2011" 是展开的根节点,其中 "Customer Project1" 和 "Customer Project 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;
    }
}

}

您必须定义层次结构列。

所以只需替换

treeGrid.addColumn( Project::getName ).setHeader( "Project Name" ).setId( "name-column" );

treeGrid.addHierarchyColumn( Project::getName ).setHeader( "Project Name" ).setId( "name-column" );