Skip to content

Grid doesn't update when not visible and after provider returns an empty collection #4937

Closed
vaadin/web-components
#5888

Description

@slargollo

Description

I'm trying to implement a view that will display a grid when the user enters parameters to filter the results.
The grid is only displayed after the user fills a form and clicks on the query button. But I don't want to display the grid if there's nothing to show.
The problem occurs when, between queries that returns results, there is a query that returns an empty value.
For instance, if the user makes a query that returns them item 01 (the grid is displayed), than another query that is empty (the grid is not displayed), then a query that returns them Item 02 (the grid is displayed), the grid doesn't update the view with Item 02, but instead continues showing Item 01.
This will not happen when the grid is always visible or when the second query returns a greater number of records then the first query. But will always happen when the second query returns only 1 record, even if the first query returns more than one (in this case, the first item of the first query is shown).

Expected outcome

RefreshAll when returning an empty collection should clear the grid view even when the grid is not visible.

Minimal reproducible example

@PageTitle("Main")
@Route(value = "")
public class MainView extends VerticalLayout {

	private static final long serialVersionUID = 2333836753838923822L;
	
	private int type = 1;
	
	private final Grid<Item> grid;

	private List<Item> source;

	private GridLazyDataView<Item> dataView;

	private Button query1;

	private Button empty;

	private Button query2;

	private Button reset;

	public MainView() {

		this.grid = new Grid<>(Item.class);
		this.dataView = this.grid.setItems(new CallbackDataProvider<>(this::onFetch, this::onCount));
		this.grid.setVisible(false);

		var layout = new HorizontalLayout();

		this.query1 = new Button("Query 1");
		this.query1.setVisible(true);
		this.query1.addClickListener(e -> {
			this.type = 1;
			this.dataView.refreshAll();
			this.queryChanged(true);
		});
		this.empty = new Button("Empty");
		this.empty.setVisible(true);
		this.empty.addClickListener(e -> {
			this.type = 0;
			this.dataView.refreshAll();
			Notification.show("No record found.", 5000, Position.TOP_END);
		});
		this.query2 = new Button("Query 2");
		this.query2.setVisible(true);
		this.query2.addClickListener(e -> {
			this.type = 2;
			this.dataView.refreshAll();
			this.queryChanged(true);
		});
		this.reset = new Button("Reset");
		this.reset.setVisible(false);
		this.reset.addClickListener(e -> {
			this.queryChanged(false);
		});

		layout.add(this.query1, this.query2, this.empty, this.reset);
		this.add(layout, this.grid);
	}
	
	private void queryChanged(boolean showGrid) {
		this.grid.setVisible(showGrid);
		this.query1.setVisible(!showGrid);
		this.empty.setVisible(!showGrid);
		this.query2.setVisible(!showGrid);
		this.reset.setVisible(showGrid);
	}

	public Stream<Item> onFetch(Query<Item, Void> query) {
		query.getPage();
		query.getPageSize();
		this.source = new ArrayList<>();
		if( this.type > 0 ) {
			this.source.add(new Item() {{ 
				this.setId(MainView.this.type);
				this.setText("Text " + MainView.this.type);
			}});
		}
		return this.source.stream();
	}

	public int onCount(Query<Item, Void> query) {
		if( this.type > 0 ) {
			return 1;
		}
		return 0;
	}

	@Data
	@EqualsAndHashCode(onlyExplicitlyIncluded = true)
	public class Item {
	
		@EqualsAndHashCode.Include
		int id;
		
		String text;
		
	}
		
}

Steps to reproduce

The sequence Query1 -> Reset -> Empty -> Query2 doesn't update the grid with the new data. Instead, the record returned by Query1 is displayed again, instead the one returned from Query2.
This doesn't happen if the grid is always visible.

Environment

Vaadin version(s): 24.0.4
OS: Ubuntu 22.04.2 LTS

Browsers

Chrome, Firefox

Activity

  1. knoobie commented on Apr 17, 2023

    @knoobie
    Contributor

    Probably duplicate of #4776

  2. sissbruecker commented on Apr 20, 2023

    @sissbruecker
    Member

    Can reproduce the issue. Also couldn't reproduce it in v23.3, so this seems to be a regression in 24.

  3. removed their assignment
    on Apr 20, 2023
  4. self-assigned this
    on May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions