Filtering Containers - Best Practices?

I have a TreeTable and use a HierarchicalContainer to bind the data. I then implemented some filtering logic to filter the rows presented in the treetable. Now I want to be able to drag and drop rows within the TreeTable to organize the data.

What I noticed is if I dnd from one row to another, in my case the business logic may require it to create additional rows (new rows) and delete other rows. But the Container logic does not allow you to add an item to a filtered container. Specifically, on the addItem() method, it checks to make sure that the item passes the filters, but if you are filtering on the data in the item, it has not had time to bind yet.

I am able to work around this by

  1. saving the Filters in a temporary array
  2. removing all filters from the container
  3. make my changes
  4. reapply the filters and call filterAll()

So, I have it working - but all this futzing around makes me wonder what the intended use of the filtered containers in this case was by the developers?

Is there some other method I should be using?

Thanks

Hi,

the filter logic does indeed seem to prevent adding items which do not pass the currently applied filters. I can’t see any way to work around it so your current approach seems valid, although I don’t think you should need to call filterAll(), readding the filters should be enough to make the set of visible items change.

Also, I don’t immediately see a reason why adding non-passing items has been prevented. Maybe you could create an enhancement request to the vaadin trac to see if this issue could be fixed in a future version.

The reason I call filterAll() was because calling addContainerFilter only takes a single filter at a time and not an array. So if you have n filters, you would end up calling filterAll - n times.

But I can optimize it a little bit by calling:


List<Filter> filters = ArrayUtil.asList(getFilters());
removeAllFilters();
// Do my biz logic here
getFilters().addAll(filters);
filterAll();

Maybe it would make no preceptable change in performance, but it should be slightly more efficient.

Added Ticket: http://dev.vaadin.com/ticket/11843

Thanks

Note that the javadoc for getFilters() says the returned collection should not be modified. With some containers, there might not be anything explicitly blocking that, but you modifying it will not work on all containers and could lead to problems on those for which it happens to work at the moment.

Of course, there could be an API for adding multiple filters at once - either at the level of specific containers or maybe even the interface Filterable, although that would break other container implementations.