Directory

← Back

FilteringTable

Extension of Vaadin Table, provides a filter bar below the table header

Author

Contributors

Rating

FilterTable is an extension of the Table component in Vaadin. FilterTable is available for both Vaadin 6 and Vaadin 7 (versions postfixed with '.v7').

Added features: • Provides automatically generated filter components between the table header and body • Automatically filters enum, boolean and date properties • Other properties are filtered as a string • Numeric properties can optionally be filtered with a comparator popup • Allows developer to provide own filter implementation for any property • Allows developer to decorate the numeric, date, enum and boolean filter components with custom captions and icons • Provides integration of PagedTable add-on with the filter bar • Provides integration of TreeTable with the filter bar

NOTE: The Vaadin 6 version of this addon requires Vaadin 6.8.7 or later, starting from add-on version 0.8.4.

NOTE: The Vaadin 7 version of this addon requires Vaadin 7.0.1 or later, starting from add-on version 0.8.4.v7.

NOTE: The Vaadin 7 version of this addon requires Vaadin 7.1.0 or later, starting from add-on version 0.9.0.v7.

NOTE: The Vaadin 7 version of this addon requires Vaadin 7.2.0 or later, starting from add-on version 0.9.9.v7.

NOTE: The Vaadin 7 version of this addon requires Vaadin 7.3.0 or later, starting from add-on version 0.9.11.v7.

Known issues: Please see the issue list in the linked GitHub page.


Please report any issues in the linked forum thread or GitHub - not the review comments. Thank you.


Please note that only the latest versions of FilteringTable for both Vaadin 6 and Vaadin 7 are supported. I will not backport bugfixes to earlier releases.


Sample code

package com.example.filtertabledemo;

import org.tepi.filtertable.FilterGenerator;

import com.vaadin.data.Container.Filter;
import com.vaadin.data.util.filter.Compare;

public class DemoFilterGenerator implements FilterGenerator {

    @Override
    public Filter generateFilter(Object propertyId, Object value) {
        if ("id".equals(propertyId)) {
            /* Create an 'equals' filter for the ID field */
            if (value != null && value instanceof String) {
                try {
                    return new Compare.Equal(propertyId,
                            Integer.parseInt((String) value));
                } catch (NumberFormatException ignored) {
                    // If no integer was entered, just generate default filter
                }
            }
        }
        // For other properties, use the default filter
        return null;
    }

}
package com.example.filtertabledemo;

import java.util.Locale;

import org.tepi.filtertable.FilterDecorator;

import com.example.filtertabledemo.FiltertabledemoApplication.State;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;

public class DemoFilterDecorator implements FilterDecorator {

    @Override
    public String getEnumFilterDisplayName(Object propertyId, Object value) {
        if ("state".equals(propertyId)) {
            State state = (State) value;
            switch (state) {
            case CREATED:
                return "Order has been created";
            case PROCESSING:
                return "Order is being processed";
            case PROCESSED:
                return "Order has been processed";
            case FINISHED:
                return "Order is delivered";
            }
        }
        // returning null will output default value
        return null;
    }

    @Override
    public Resource getEnumFilterIcon(Object propertyId, Object value) {
        if ("state".equals(propertyId)) {
            State state = (State) value;
            switch (state) {
            case CREATED:
                return new ThemeResource("../runo/icons/16/document.png");
            case PROCESSING:
                return new ThemeResource("../runo/icons/16/reload.png");
            case PROCESSED:
                return new ThemeResource("../runo/icons/16/ok.png");
            case FINISHED:
                return new ThemeResource("../runo/icons/16/globe.png");
            }
        }
        return null;
    }

    @Override
    public String getBooleanFilterDisplayName(Object propertyId, boolean value) {
        if ("validated".equals(propertyId)) {
            return value ? "Validated" : "Not validated";
        }
        // returning null will output default value
        return null;
    }

    @Override
    public Resource getBooleanFilterIcon(Object propertyId, boolean value) {
        if ("validated".equals(propertyId)) {
            return value ? new ThemeResource("../runo/icons/16/ok.png")
                    : new ThemeResource("../runo/icons/16/cancel.png");
        }
        return null;
    }

    @Override
    public Locale getLocale() {
        // will use the application locale
        return null;
    }

    @Override
    public String getFromCaption() {
        return "Start date:";
    }

    @Override
    public String getToCaption() {
        return "End date:";
    }

    @Override
    public String getSetCaption() {
        // use default caption
        return null;
    }

    @Override
    public String getClearCaption() {
        // use default caption
        return null;
    }

}
package com.example.filtertabledemo;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import org.tepi.filtertable.FilterTable;

import com.vaadin.Application;
import com.vaadin.data.Container;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class FiltertabledemoApplication extends Application {

    /**
     * Example enum for enum filtering feature
     */
    public enum State {
        CREATED, PROCESSING, PROCESSED, FINISHED;
    }

    private FilterTable filterTable;

    @Override
    public void init() {
        Window mainWindow = new Window("FilterTable Demo Application");
        setMainWindow(mainWindow);

        /* Create FilterTable */
        filterTable = buildFilterTable();

        VerticalLayout mainLayout = new VerticalLayout();
        mainLayout.setSizeFull();
        mainLayout.setSpacing(true);
        mainLayout.setMargin(true);
        mainLayout.addComponent(filterTable);
        mainLayout.setExpandRatio(filterTable, 1);
        mainLayout.addComponent(buildButtons());

        mainWindow.setContent(mainLayout);
    }

    private FilterTable buildFilterTable() {
        FilterTable filterTable = new FilterTable();
        filterTable.setSizeFull();
        filterTable.setFilterDecorator(new DemoFilterDecorator());
        filterTable.setFilterGenerator(new DemoFilterGenerator());
        filterTable.setContainerDataSource(buildContainer());
        filterTable.setFiltersVisible(true);
        return filterTable;
    }

    private Component buildButtons() {
        HorizontalLayout buttonLayout = new HorizontalLayout();
        buttonLayout.setSizeUndefined();
        buttonLayout.setSpacing(true);
        Button showFilters = new Button("Show filter bar");
        showFilters.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                filterTable.setFiltersVisible(true);
            }
        });
        buttonLayout.addComponent(showFilters);
        Button hideFilters = new Button("Hide filter bar");
        hideFilters.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                filterTable.setFiltersVisible(false);
            }
        });
        buttonLayout.addComponent(hideFilters);
        return buttonLayout;
    }

    private Container buildContainer() {
        IndexedContainer cont = new IndexedContainer();
        Calendar c = Calendar.getInstance();

        cont.addContainerProperty("name", String.class, null);
        cont.addContainerProperty("id", Integer.class, null);
        cont.addContainerProperty("state", State.class, null);
        cont.addContainerProperty("date", Date.class, null);
        cont.addContainerProperty("validated", Boolean.class, null);

        Random random = new Random();
        for (int i = 0; i < 10000; i++) {
            cont.addItem(i);
            /* Set name and id properties */
            cont.getContainerProperty(i, "name").setValue("Order " + i);
            cont.getContainerProperty(i, "id").setValue(i);
            /* Set state property */
            int rndInt = random.nextInt(4);
            State stateToSet = State.CREATED;
            if (rndInt == 0) {
                stateToSet = State.PROCESSING;
            } else if (rndInt == 1) {
                stateToSet = State.PROCESSED;
            } else if (rndInt == 2) {
                stateToSet = State.FINISHED;
            }
            cont.getContainerProperty(i, "state").setValue(stateToSet);
            /* Set date property */
            cont.getContainerProperty(i, "date").setValue(c.getTime());
            c.add(Calendar.DAY_OF_MONTH, 1);
            /* Set validated property */
            cont.getContainerProperty(i, "validated").setValue(
                    random.nextBoolean());
        }
        return cont;
    }
}

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Initial version of FilteringTable for Vaadin 7 RC.

Known issues:

  • Filtering does not work in PagedTable or TreeTable
  • PopupButton is not yet V7-compatible, so Date/Numeric popup filters are not available
  • Focused filter field loses focus after a filter has been applied

Please try this out and report all issues in the Forum thread. Thanks!

Released
2013-01-17
Maturity
EXPERIMENTAL
License
Apache License 2.0

Compatibility

Framework
Vaadin 7.0
Vaadin 6.7+ in 0.5.0
Vaadin 6.0+ in 0.5.3
Vaadin 6.8+ in 0.6.0
Vaadin 7.1 in 0.9.0.v7
Vaadin 7.3+ in 0.9.10.v7
Vaadin 7.5+ in 0.9.9.v7
Vaadin 7.6+ in 0.9.14.v7
Vaadin 8.0+ in 1.0.0.v8
Vaadin 7.7+ in 1.0.0.v7
Browser
Internet Explorer
Firefox
Opera
Safari
Google Chrome
Internet Explorer
Internet Explorer
Online