【问题标题】:Not able to make Vaadin Tree Grid work with File System Data Provider无法使 Vaadin Tree Grid 与文件系统数据提供程序一起使用
【发布时间】:2021-12-26 11:35:22
【问题描述】:

我有一个要求,其中我需要显示一个包含用户主文件夹层次结构的树,其中包括文件和文件夹。为此,我一直在尝试使用 Vaadin TreeGrid 和 FileSystemDataProvider

我正在使用 Vaadin 21.0.0 并使用此依赖项

<dependency>
    <groupId>org.vaadin.filesystemdataprovider</groupId>
    <artifactId>filesystemdataprovider</artifactId>
    <version>3.0.0</version>
</dependency>

代码如下

String path = System.getProperty("user.home");
File rootFile = new File(path);
FilesystemData root = new FilesystemData(rootFile, false);
FilesystemDataProvider fileSystem = new FilesystemDataProvider(root);
tree.setDataProvider(fileSystem);
add(tree)

但是,treegrid 在运行程序时显示空白(不显示树结构)。需要你的帮助,我可能做错了什么

【问题讨论】:

    标签: vaadin vaadin-flow vaadin-grid


    【解决方案1】:

    您需要将 TreeGrid 配置为具有列,例如:

    public GridView() {
        File rootFile = new File(path);
        FilesystemData root = new FilesystemData(rootFile, false);
        FilesystemDataProvider fileSystem = new FilesystemDataProvider(root);
        TreeGrid<File> tree = new TreeGrid<>();
        tree.setDataProvider(fileSystem);     
        tree.addHierarchyColumn(file -> file.getName()).setHeader("Name");
        tree.addColumn(file -> file.length()).setHeader("Size");
        tree.setWidth("750px");
        tree.setHeight("500px");
        setSizeFull();
        setAlignItems(Alignment.CENTER);
        setJustifyContentMode(JustifyContentMode.CENTER);
        add(tree);
    }
    

    【讨论】:

      【解决方案2】:

      如果您没有为 Grid/TreeGrid 的构造函数提供 bean 类,则必须通过 addColumnaddHierarchyColumn 方法之一显式添加列。如果没有以这种方式添加列,则 Grid 将在页面上呈现为空白。

      【讨论】:

        猜你喜欢
        • 2014-04-10
        • 1970-01-01
        • 2017-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-11
        相关资源
        最近更新 更多