Add Item with GridListDateView and filter, using Project reactor

Hello, is it possible to add item by item with the addItem method of the GridListaDataView and then be able to Filter ?

I can not, filter I get NullPointerException, add each one item yes,

this.subscriber.flatMap(devices -> Flux.fromStream(devices.stream())
                .map(device -> this.mapDevice(device, ui)))
        .flatMap(Function.identity())
        .subscribe(entity -> {
            ui.access(() -> {
                log.info("Entity {}", entity);
                this.gridListDataView = this.grid.getListDataView();
                if(!this.gridListDataView.contains(entity)) {
                    this.gridListDataView.addItem(entity);
					this.gridListDataView.addFilter(item -> {
						final String name = searchField.getValue().trim();
						if (name.isEmpty()) {
							return true;
						}
						return StringUtils.containsIgnoreCase(item.getPin(), name);
					});
                }
            });
        });

My problem is that I come from a Flux with a mongo repository reactive version. and then I can’t filter.

@Value
@Builder
@ToString
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class TestDto {

    @EqualsAndHashCode.Include
    String pin;

}	

I’d suggest you to try calling this.gridListDataView.addFilter() in the searchField component listener instead and only call addItem in the subscribe callbacks.

The NPE reason here is probably that addFilter calls refreshAll internally and some items are null at that point or pin is null.

thanks but nothing hommie

java.lang.NullPointerException: Cannot invoke "com.vaadin.flow.function.SerializableConsumer.accept(Object)" because "inMemoryFilter" is null
	at com.vaadin.flow.component.grid.Grid.updateInMemoryFiltering(Grid.java:5072) ~[vaadin-grid-flow-24.5.8.jar:na]
	at com.vaadin.flow.component.grid.Grid.onInMemoryFilterOrSortingChange(Grid.java:5058) ~[vaadin-grid-flow-24.5.8.jar:na]
	at com.vaadin.flow.data.provider.AbstractListDataView.fireFilteringOrSortingChangeEvent(AbstractListDataView.java:405) ~[flow-data-24.5.9.jar:24.5.9]

searchField.addValueChangeListener(event -> {
            if (Objects.nonNull(event.getValue())) {
                Animated.animate(grid, Animated.Animation.FADE_IN);
                this.gridListDataView.addFilter(item -> {
                    final String name = searchField.getValue().trim();
                    if (name.isEmpty()) {
                        return true;
                    }
                    return StringUtils.containsIgnoreCase(item.getPin().getPin(), name);
                });
                spanRelaysCount.setText(String.valueOf(this.gridListDataView.getItems().count()).concat(" Total"));
                spanRelaysCount.setVisible(true);
            }
        });

Now I see, thanks.
A possible reason for having inMemoryFilter null is that you never call grid.setItems or grid.setDataProvider before you call addFilter.

Could you please try to call grid.setItems(); before adding a value change listener? This should add an empty list of items to grid and should prevent grid to throw NPE.

This is by the way good use case. I believe Flow can create an empty items list and data provider if it’s not defined at the time you call getListDataView().anyMethod().

1 Like

With what you have told me, the filtering now works.

But there will be a way to improve this part, won’t it ?

Yes, this should be improved.
I’ve made a ticket for it ListDataView throws NPE when no items or data provider have been set before adding a filter · Issue #20828 · vaadin/flow · GitHub.

1 Like