gpt4 book ai didi

Vaadin TreeGrid 没有展开或折叠图标

转载 作者:行者123 更新时间:2023-12-04 02:50:10 25 4
gpt4 key购买 nike

我使用 Vaadin 创建了一个包含单个 TreeGrid 的简单 Web 应用程序。显示了 TreeGrid,但没有用于展开或折叠根元素(“2010 年”或“2011 年”)的句柄,尽管有子元素。当我使用 expand(..) 方法时,树被展开(如屏幕截图所示),但没有图标可以折叠它。在下面的屏幕截图中,“2011 年”是展开的根节点,其中“Customer Project1”和“Customer Project 2”是子节点。该项目是使用 Maven 构建的,我使用 vaadin-bom 版本 13.0.2 和 vaadin-grid-flow-3.0.3.jar。屏幕截图下方是我使用的代码。我认为问题一定出在方法 createTreeGrid() 的某个地方。可以在此处找到 Vaadin 的工作示例 Vaadin example

那么有人对如何解决这个问题有任何想法吗?任何帮助表示赞赏... :-)

托斯滕

Screenshot

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");

关于Vaadin TreeGrid 没有展开或折叠图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55392653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com