【问题标题】:Filter tree in VaadinVaadin 中的过滤树
【发布时间】:2011-07-25 14:47:32
【问题描述】:

我想根据某些编辑框中的文本(在文本更改时)从 vaadin 树中隐藏叶子。 即,如果编辑框中的文本是“ab”,我只想显示以“ab”开头的文本的叶子。 如果文本为空,我想显示所有叶子。

我该怎么做?

【问题讨论】:

    标签: java tree filtering vaadin


    【解决方案1】:

    您必须过滤附加到树的数据容器。

    6.6.0 版中引入了一个新的过滤器 API,它允许您创建自定义过滤器。我还没有尝试过新的 API,但在你的情况下它应该像这样工作:

    textField.addListener(new FieldEvents.TextChangeListener() {
        void textChange(FieldEvents.TextChangeEvent event) {
            // Remove existing filter (if any).
            // This is OK if you don't use any other filters, otherwise you'll have to store the previous filter and use removeContainerFilter(filter)
            dataContainer.removeAllContainerFilters();
    
            // Create a new filter which ignores case and only matches String prefix
            SimpleStringFilter filter = new SimpleStringFilter(propertyId, event.getText(), true, true);
    
            // Add the new filter
            dataContainer.addContainerFilter(filter);
        }
    });
    

    其中 textField 是您的“编辑框”,dataContainer 是附加到树的数据容器,properyId 是属性 ID包含要过滤的文本的容器字段。

    请注意,以上代码未经测试,因为我目前无法访问相应的开发工具。

    【讨论】:

    • tnx,但是如果我没有与表关联的 dataContainer 怎么办?我的意思是,如果我使用 table.additem() 方法显式添加项目会怎样。我怎样才能在这里得到dataContainer?
    • 总有一个数据容器。如果您不提供,Table 将创建它(IIRC IndexedContainer)。您可以通过 table.getContainerDataSource() 访问它。
    • Tnx,但是使用该方法我看不到没有删除/添加过滤器方法:(我猜默认容器不适用于过滤器编辑 - 我想我通过执行此 IndexedContainer 容器使其工作= (IndexedContainer) valuesTable.getContainerDataSource();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 2020-06-06
    • 2012-11-07
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    相关资源
    最近更新 更多