0

here is a good thread about DataProvider.refreshAll() on Vaadin 8.5.1, but it doesn't seem to work this way in Vaadin 11.

I used this starter app to play around. It displays some imaginary product data in a grid.

At first, I added a refresh command to SampleCrudView:

public HorizontalLayout createTopBar() {
    ...
    HorizontalLayout topLayout = new HorizontalLayout();
    Button btn = new Button("refresh");
    btn.addClickListener(event -> dataProvider.refreshAll());
    topLayout.add(btn);
    ...
    return topLayout;
}

The folks from vaadin override getId() in their ProductDataProvider like this to use it as an object identifier:

@Override
public Integer getId(Product product) {
    Objects.requireNonNull(product,
            "Cannot provide an id for a null product.");

    return product.getId();
}

That ProductDataProvider extends ListDataProvider, which is initialized on startup with data from MockDataService, so that we always deal with the same objects. I changed that:

public class MockDataService extends DataService {
...
@Override
public synchronized List<Product> getAllProducts() {
    //added ->
    MockDataGenerator.resetProductCounter(); //this one sets nextProductId = 1
    products = MockDataGenerator.createProducts(categories);
    products.stream().forEach(p -> System.out.println(p.getId() + ", " + p.getProductName()));
    //<- added
    return products;
}

So now you will get new Product instances within the same ID range every time you call getAllProducts():

  public class ProductDataProvider extends ListDataProvider<Product> {
 ...

 @Override
 public Stream<Product> fetch(Query<Product, SerializablePredicate<Product>> query) {
     //added ->
    this.getItems().clear();
    this.getItems().addAll(DataService.get().getAllProducts());
     //<- added
     return super.fetch(query); 
 }   

So the point is, this doesn't work - the data in the grid is still the same after "refresh" has been clicked.

Any suggestions?

Regards,

m_OO_m

4

1 回答 1

1

This is caused by a bug that was fixed a couple a days ago. The fix will be included in the next maintenance release.

于 2018-12-05T06:33:51.680 回答