Grid filter didn't work when dataview is updated

Following the Grid Filtering example, I made a Grid where it loads the data and filters correctly, however when I update the list and the data in the Grid, the filters stop working, does anyone know what I am doing wrong?

private Grid<CampaignScheduledDTO> createCampaignGrid() {
        log.debug("Loading. . . . createCampaignGrid");

        campaignScheduledGrid = new Grid<>(CampaignScheduledDTO.class, false);
        campaignScheduledGrid.setWidth("100%");
        campaignScheduledGrid.getStyle().set("flex-grow", "0");

        List<CampaignScheduledDTO> scheduleByDate = loadCampaignScheduleService(actualLine);

        Grid.Column<CampaignScheduledDTO> activeColumn = campaignScheduledGrid.addComponentColumn(CampaignScheduledDTO -> createStatusIcon(CampaignScheduledDTO.getActive()));
        Grid.Column<CampaignScheduledDTO> stretchlineColumn = campaignScheduledGrid.addColumn(CampaignScheduledDTO::getStretchlineFk).setSortable(true);
        Grid.Column<CampaignScheduledDTO> nameColumn = campaignScheduledGrid.addColumn(CampaignScheduledDTO::getName).setWidth("230px").setFlexGrow(0).setSortable(true);
        Grid.Column<CampaignScheduledDTO> productFkColumn = campaignScheduledGrid.addColumn(CampaignScheduledDTO::getProductFk).setSortable(true);
        Grid.Column<CampaignScheduledDTO> cOutputColumn = campaignScheduledGrid.addColumn(CampaignScheduledDTO::getCOutput).setSortable(true);
        Grid.Column<CampaignScheduledDTO> speedColumn = campaignScheduledGrid.addColumn(CampaignScheduledDTO::getSpeed).setSortable(true);
        Grid.Column<CampaignScheduledDTO> tonnageColumn = campaignScheduledGrid.addColumn(CampaignScheduledDTO::getTonnage).setSortable(true);
        Grid.Column<CampaignScheduledDTO> mrschedCountColumn = campaignScheduledGrid.addColumn(CampaignScheduledDTO::getMrschedCount).setSortable(true);

        campaignScheduledDataView = campaignScheduledGrid.setItems(scheduleByDate);
        CampaignScheduledFilter campaignScheduledFilter = new CampaignScheduledFilter(campaignScheduledDataView);

        campaignScheduledGrid.getHeaderRows().clear();
        HeaderRow headerRow = campaignScheduledGrid.appendHeaderRow();

        headerRow.getCell(activeColumn).setComponent(
                createFilterHeader(" ", campaignScheduledFilter::setActive));
        headerRow.getCell(stretchlineColumn).setComponent(
                createFilterHeader(getTranslation("hexagon.mcr.campaign.grid.column.line"), campaignScheduledFilter::setStretchlineFk));
        headerRow.getCell(nameColumn).setComponent(
                createFilterHeader(getTranslation("hexagon.mcr.campaign.grid.column.name"), campaignScheduledFilter::setName));
        headerRow.getCell(productFkColumn).setComponent(
                createFilterHeader(getTranslation("hexagon.mcr.campaign.grid.column.product"), campaignScheduledFilter::setProductFk));
        headerRow.getCell(cOutputColumn).setComponent(
                createFilterHeader(getTranslation("hexagon.mcr.campaign.grid.column.output"), campaignScheduledFilter::setCOutput));
        headerRow.getCell(speedColumn).setComponent(
                createFilterHeader(getTranslation("hexagon.mcr.campaign.grid.column.speed"), campaignScheduledFilter::setSpeed));
        headerRow.getCell(tonnageColumn).setComponent(
                createFilterHeader(getTranslation("hexagon.mcr.campaign.grid.column.tonnage"), campaignScheduledFilter::setTonnage));
        headerRow.getCell(mrschedCountColumn).setComponent(
                createFilterHeader(getTranslation("hexagon.mcr.campaign.grid.column.count"), campaignScheduledFilter::setMrschedCount));

        campaignScheduledGrid.addSelectionListener(selection -> {
            Optional<CampaignScheduledDTO> optionalCampaign = selection.getFirstSelectedItem();
            if (optionalCampaign.isPresent()) {
                campaignSelected = optionalCampaign.get().getName();
                updateMillrollScheduledData(campaignSelected);
                log.debug("campaignScheduledGrid -> campaignSelected: {}", campaignSelected);
            }
        });
        return campaignScheduledGrid;
    }

    private void updateCampaignScheduledGridData(String line) {
        log.debug("Loading. . . . updateCampaignScheduledGridData");
        log.trace("(Line -> {})", line);

        List<CampaignScheduledDTO> scheduleByDate = loadCampaignScheduleService(line);
        campaignScheduledDataView = campaignScheduledGrid.setItems(scheduleByDate);
        campaignScheduledGrid.getDataProvider().refreshAll();

    }

As you can see the code is very similar to the example, the updateCampaignScheduledGridData method is executed by a Select, In the method from a service it loads the data and puts it in the DataView, the information is updated correctly in the grid, but the filters are no longer working.

What can I do? Thank you very much for your help

You use setItems to set the new data and that returns a new DataView instance, but you do not instantiate a new filter class with that new DataView, you continue to use the old one.

I made the CampaignScheduledFilter campaignScheduledFilter as a global variable, and in the method that update the grid:

    private void updateCampaignScheduledGridData(String line) {
        log.debug("Loading. . . . updateCampaignScheduledGridData");
        log.trace("(Line -> {})", line);

        List<CampaignScheduledDTO> scheduleByDate = loadCampaignScheduleService(line);
        campaignScheduledDataView = campaignScheduledGrid.setItems(scheduleByDate);
        campaignScheduledGrid.getDataProvider().refreshAll();
        campaignScheduledGrid.deselectAll();
        campaignScheduledFilter = new CampaignScheduledFilter(campaignScheduledDataView);
    }

But still the filters don’t work :frowning:

I’m bit ashamed of the quality of the linked doc example, I think it forwards people to do wrong things. I pretty much never call refreshAll or any DataProvider or Dataview APIs, and I have never needed to use DataView.setFilter method. I think you are better off sticking to standard Java features.

I prepare this example as an alternative approach (which I plan to submit later as a PR to our docs, thus not even using the VGrid, but sticking to standard Vaadin components):

1 Like

What you mean by “stops working”. Do you mean that existing filters are not carried over when you set new items? That is expected behavior as filters are part of the DataView and DataProvider. setItems method will create a new data provider and data view, which naturally do not have any filters set yet. If you want to apply the same filters to the new data you need to copy the values of the filter from the old first.

Thanks for your answer, I have tried to create again the filters and apply them to the grid, but I don’t get any result, it doesn’t filter.

And I can’t find an example to know if maybe I’m doing it wrong.

Thank you very much for your answer!!, you are absolutely right about the examples, this one is much clearer. For a moment I was confused when I tried to modify the class to change the data, then I remembered that it is basically the same as a Grid, and I made it work easily.

I love Viritin project, it have a lot of things that should or could be in the Vaadin core, when I get out of the current project and I have free time I will surely collaborate with it. Thank you very much for your help. :D you solved my week.