Framework: Grid should have a function to get the total number of rows

Created on 24 Feb 2017  路  9Comments  路  Source: vaadin/framework

Grid (Vaadin 8) has no function to ask for the complete number of data rows.
We need such a function to visualize the row count of the Grid.
Optional Filters should be taken into account.
see https://vaadin.com/forum#!/thread/15252122

enhancement

Most helpful comment

As a developer, I want to have two methods for (List)dataProvider:

  1. .getSize() // Complete size
  2. getFilteredSize() // Size of items shown in grid (after filtering)

In addition, one could have access to actual filtered item list e.g. getFilteredItems()

This would really simplify things
Thanks

All 9 comments

Hi Matti,
thanks for the patch.
if I have read the patch right, the new size() function does not return the filtered number of rows but the complete amount of rows of the DataProvider without filtering. This could not be the visible count of rows.
public int size() { return getDataProvider().size(new Query<>()); }
I would need the filtered, actual visible amount of rows from the Grid. I do not know, whether a size() function, which does not take the filter into account, would be that usefull.
Alex

Very good point @aley2003 , and there are also other implications too with the version in the PR, discussed in there. I'll do another fix for this tomorrow.

This proved a lot more complex and is on hold for now. The main issue is:

  • For any time there are changes in the data size for grid, the updated size is not fetched until the _beforeClientResponse_ method is run, thus after changes the updates are not reflected immediately. Meaning that e.g.
Grid<String> grid = new Grid<>();
grid.setItem("foo","bar");
grid.size(); // would not return 2

Also Matti's approach of doing a new size query for each grid.size() call would not reflect the actual size of grid either, since the data provider size can have changed but the grid doesn't necessarily know this. And it was also lacking the filtering information.

That is why I propose that there should instead be an event that is fired each time when the size of the data is changed.

As a developer I just want to get the size of the grid, I don't care if it causes an extra query at some point and in most common use cases (in memory container) the size is correct. I hope you are not trying to make this too "academic". Simple things should be simple.

@mstahv I'm just trying to make it _correct_. I'm not going to add any feature that is broken from the beginning. Doing a size query reports the size of the backend at that moment, not the amount of rows in the grid.

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.

As a developer, I want to have two methods for (List)dataProvider:

  1. .getSize() // Complete size
  2. getFilteredSize() // Size of items shown in grid (after filtering)

In addition, one could have access to actual filtered item list e.g. getFilteredItems()

This would really simplify things
Thanks

So I have been using this static helper method I made for a while now and I can say from my experience it does report the number of filtered items from a grid:
public static final void updateCaption(Grid grid){
grid.setCaption(
grid.getCaption()
.replaceFirst("\\{\\}", "0")
.replaceFirst("\\d+",
String.valueOf( grid.getDataProvider().size(new Query<>()))));
}

That being said I also have a whole, hand coded binder-esque class that handles filtering (builds the fields too).
I will say that if you are using ListDataProviders for the grid then:
grid.getDataProvider().size(new Query<>())
will report the correct number for filtered Items.

Example:
image
filterSelect.addValueChangeListener(e->GridUtil.updateCaption(grid));
image

Now as for non-ListDataProviders, I will have to look into more.

Hello there!

It looks like this issue hasn't progressed lately. There are so many issues that we just can't deal them all within a reasonable timeframe.

There are a couple of things you could help to get things rolling on this issue (this is an automated message, so expect that some of these are already in use):

  • Check if the issue is still valid for the latest version. There are dozens of duplicates in our issue tracker, so it is possible that the issue is already tackled. If it appears to be fixed, close the issue, otherwise report to the issue that it is still valid.
  • Provide more details how to reproduce the issue.
  • Explain why it is important to get this issue fixed and politely draw others attention to it e.g. via the forum or social media.
  • Add a reduced test case about the issue, so it is easier for somebody to start working on a solution.
  • Try fixing the issue yourself and create a pull request that contains the test case and/or a fix for it. Handling the pull requests is the top priority for the core team.
  • If the issue is clearly a bug, use the Warranty in your Vaadin subscription to raise its priority.

Thanks again for your contributions! Even though we haven't been able to get this issue fixed, we hope you to report your findings and enhancement ideas in the future too!

Could we not cache the row count in the com.vaadin.data.provider.DataCommunicator and return it by a method in the Grid? As much as I found this is the place where DataProvider.size() is called. We could also fire an event, so "count labels" could be updated.

Was this page helpful?
0 / 5 - 0 ratings