1 year ago

#342764

test-img

WeekendJedi

How do I get my grid to refresh whenever I visit the view that accesses it?

I have a Grid in my Vaadin project that displays portions of all the Events entities in my EventsRepository. The view that shows this grid shows the Events entities just fine, but only in their first state once they enter the repository. Whenever a user signs up for an event, its "participants" count is supposed to drop, and their name is supposed to be added to the user list (just a large String for now). However, whenever I go back to this view, the grid is not updated. I know that I need to be calling grid.getDataProvider().refreshAll(); somewhere in my code for the grid to update with these changes, but I just can't figure out where to put it. I have confirmed that I am properly changing these values, but these changes just are not being reflected in the grid.

Here is my view that accesses the grid I mention:

import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.AfterNavigationListener;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import ymca.tracker.application.data.entity.Events;
import ymca.tracker.application.data.service.EventsService;

import java.util.List;

@PageTitle("Registration Management")
@Route(value = "registration-management")
public class RegistrationManagementView extends Div implements AfterNavigationListener {

    EventsService eventsService;
    Grid<Events> grid = new Grid<>();

    public RegistrationManagementView(EventsService eventsService) {
        this.eventsService = eventsService;
        addClassName("account-view");
        setSizeFull();
        grid.setHeight("100%");
        grid.addThemeVariants(GridVariant.LUMO_NO_BORDER, GridVariant.LUMO_NO_ROW_BORDERS);
        List<Events> events = getData();
        grid.setItems(events);
        grid.addComponentColumn(event -> createCard(event));
        add(grid);
        // I tried calling this line of code in the constructor, but this does not refresh either
        grid.getDataProvider().refreshAll();
    }

    private HorizontalLayout createCard(Events events) {
        HorizontalLayout card = new HorizontalLayout();
        card.addClassName("card");
        card.setSpacing(false);
        card.getThemeList().add("spacing-s");

        VerticalLayout vl = new VerticalLayout();
        vl.addClassName("description");
        vl.setSpacing(false);
        vl.setPadding(false);

        HorizontalLayout header = new HorizontalLayout();
        header.addClassName("header");
        header.setSpacing(false);
        header.getThemeList().add("spacing-s");

        Span name = new Span(events.getName());
        name.addClassName("name");

        header.add(name);

        Span capacity = new Span("Spots Remaining: " + events.getParticipants());
        capacity.addClassName("capacity");
        
        String registrantList = events.getUsers();

        Span signUps = new Span("Current Registrants: \n" + registrantList);
        signUps.addClassName("signUps");

        vl.add(header, capacity, signUps);
        card.add(vl);
        return card;
    }

    public List<Events> getData() {
        List<Events> events = eventsService.findAllEvents();
        return events;
    }

    /* I tried an aferNavigationListener, but I really don't know how 
        to implement this at all but I know this is not correct
    */
    @Override
    public void afterNavigation(AfterNavigationEvent afterNavigationEvent) {
        grid.getDataProvider().refreshAll();
    }
}

jpa

vaadin

vaadin-flow

vaadin-grid

0 Answers

Your Answer

Accepted video resources