Vaadin 8 Grid row count

Silly question but how can you get the number of data rows in a Grid? I have a text field that applies a filter to a ListDataProvider of a Grid and I would like to know how many are actually showing (so I can do an action when there is only one row and the user presses enter) but I didn’t see any getRowCount() etc on the grid and the LDP shows only the item collection and not how many of those pass the filter…

Thanks in advance,

There is no function to get rowCount.

Cool, thanks for the info. Do you know of any workarounds until it gets included?

I am also interested in a work around. Currently, the ListDataProvider has a size(query) method. Firstly, I find it unintuitive to call size with a query since most people are interested in the size based on existing filters / dataset. Secondly, isn’t it inefficient that I first apply all filters and then apply query one by one (for all filters) to get the row count (twice)?

Jeez, I am missing container already!

I didn’t try something before my first answer (I almost never use ListProvider to large filtered grids) but if you call this:
grid.getDataProvider().size(new Query<>())

It does apply the current filter, the parameter is only to apply additional filters.

I try it and I seems “ok” for me. (I’ve got the number of filtered items)

But in this ticket
Pleku said that (and I don’t know why :slight_smile: ):

Doing grid.getDataProvider().size(new Query<>()); is something that anyone can do,
but that may not return the same amount of rows that is actually in the grid if there has been changes.
If they know that this is fine, then they can do it. Since grid doesn't have its own filtering,
the developer should have the filter available anyway and can apply that to the query.

Many thanks Jean,

Let’s hope the devs don’t block this way :slight_smile:

I did a quick hack by extending the ListDataProvider and adding a long field “count” with a getter and then

      public Stream<T> fetch(Query<T, SerializablePredicate<T>> query)
         Supplier<Stream<T>> supplier = () -> super.fetch(query);
         count = supplier.get().count();
         return supplier.get();

Not sure that the timing of the count is OK for all usages but it appears to work for my case (check if there is only one row provided when user presses enter in a textfield providing filtering for the LDP)

Caching your count in the filter seems to be a very poor design and makes an assumption about how the grid uses the fetched data.

Every time the grid requests the rows from the provider, it also requests the size (filtering or not). It should notify an interested listener with the results. If the total item size is indeterminate, it should use a defined constant, or boolean value to indicate this in the callback.

At any given moment, the client is interested in the the total item count, and the filtered (visible) item count, so I would suggest a callback such as:

typedef GridSizeListener = (itemCount:Int, isFiltered:Boolean, isIndeterminate:Boolean) → Unit

This would be very easy to implement, and would require minimal CPU cycles. It would also be possible to include both the filtered and total count , with a caviat that depending on the state of the filter flag, the other count would simply be a hint of the last queried result. Of course the receiver could cache the isFiltered=true and isFiltered=false false internally.

Currently I need to call my provider a second time after I modify a filter to get this information; it is slow and IMO completely against the spirit of what the Grid represents.