开发者

Filter tree in Vaadin

I want to hide leafs from vaadin tree according to text in some editbox (on text change). ie if text in editbox is "ab" i want to show only leafs with text starting with "ab". and if text is empty, i want to show all leafs.

Ho开发者_开发问答w can i make that?


You will have to filter the data container which is attached to the tree.

A new Filter API was introduced in version 6.6.0 which allows you to create custom Filters. I haven't tried the new API yet, but in your case it should work like this:

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

in which textField is your "editbox", dataContainer is the data container which is attached to your tree and properyId is the property id of the container field which contains the text you want to filter.

Note that the above code is untested because I currently can't access the appropriate development tools.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜