Unable to update grid data after setting a filter

I seem to be stuck. I want to replace the data in a Grid after setting a filter, where by “replace” I mean remove all the existing rows and insert new rows.

I start by calling #grid.clear(); to reset it, removing all the old data.

The first problem is that when I call addRow() on the grid now, it attempts to add the new row and I get an NPE down in this code:

Object itemId = dataSource.addItem(); try { Item item = dataSource.getItem(itemId); the returned
is null because getItem() calls through several methods and eventually decides to look in filteredItemIds for the newly added item, after seeing that filteredItemIds is not null:

protected boolean isFiltered() {
    return filteredItemIds != null;

However, #grid.clear() does not set filteredItemIds to null, it sets it to an empty collection, so this always true and the code looks for the newly added item in the wrong collection (ie not the allItems collection).

There is no public accessor I can see to reset the value to null. Now, I’m thinking that I can’t replace the values in the existing container, I should replace the container with a new one to get an uninitialized filteredItemIds. But then I get an error saying that the container does not contain all the columns in the Grid:

java.lang.IllegalStateException: Found at least one column in Grid that does not
exist in the given container: coverage with the header "Coverage".
Call removeAllColumns() before setContainerDataSource() if you want to reconfigure
the columns based on the new container.

So I call removeAllColumns() as instructed, but then I get another error when i try to add the rows:

java.lang.IllegalArgumentException: There are 0 visible columns, but 15 cell values were provided. So I try to reinitialize the grid by re-adding the columns (using #grid.addColumn()) before adding new rows, but:


java.lang.IllegalStateException: Container for this Grid is not a default container
from Grid() constructor

[/code]Meaning I can’t add a new column to the grid.

I’ve been at this for hours and can’t figure it out. Please help. I’m using Vaadin 7.7.6.


I think you are trying to do this in too difficult way. I would do this in following way. Container.Filterable getItemIds() returns Collection of the filtered items. So that is a good starting point. I would loop that through and create a new container, and setup a new Grid with that new container. That process does not much add memory footprint, since you are mostly dealing with referrences and Grid itself is mostly metadata. Rest is dealt with garbage collection.

Thanks. Replacing the entire grid works.

I do think, however, that while the framework overall is very well designed, this component’s API needs work.