How to check if a row is present in vaadin’s grid after filtering

You probably have already tried out filtering feature in your vaadin project if you reached this article. Let's understand how to get the list of items left in grid after filtering got applied. We have access to grid's dataSource and we can perform:
List<Topic> filteredTopics = new ArrayList<>();
topics.forEach(topic -> {
    if (topicGrid.getContainerDataSource().containsId(topic)) {
    filteredTopics.add(topic);
    }
});
After a small modification of the code above you can check if an item got filtered out. And this way we can check if there is at least one item left in grid (can be placed into filter.addCellFilterChangedListener() ):
try {
    Object obj = topicGrid.getContainerDataSource().getIdByIndex(0);
    if (obj != null) {
        // If we have at least one item/row in the grid, then we e.g. enable
        // a button.
        button.setEnabled(true); // we have at least one item in grid
    } else {
        button.setEnabled(false);
    }
} catch (IndexOutOfBoundsException exc) {
    button.setEnabled(false);
}
(numeration of the items starts from 0). You can take a look at filtering here: filtering demo
You can leave a response, or trackback from your own site.

Leave a Reply