Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Vaadin 8 Grid row count

Nicklas Karlsson
5 years ago Mar 21, 2017 12:38pm

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, 
  Nik

Jean-Christophe Gueriaud
5 years ago Mar 21, 2017 4:24pm
Nicklas Karlsson
5 years ago Mar 21, 2017 9:32pm
Ammar Hassan
5 years ago Mar 24, 2017 9:17am

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!

Jean-Christophe Gueriaud
5 years ago Mar 24, 2017 10:15am
Ammar Hassan
5 years ago Mar 24, 2017 11:06am
Nicklas Karlsson
5 years ago Mar 25, 2017 1:49pm

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

@Override
      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)

steven spungin
4 years ago Oct 17, 2017 1:26pm

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.