每当我访问访问网格的视图时,如何刷新网格?

发布于 2025-01-17 09:32:26 字数 3339 浏览 4 评论 0原文

我的Vaadin项目中有一个网格,它显示了我的Events Repository中所有事件实体的部分。显示该网格的观点显示了事件实体很好,但只有进入存储库后才处于第一个状态。每当用户签署一个事件时,其“参与者”计数应该会删除,并且应该将其名称添加到用户列表中(目前只是一个大字符串)。但是,每当我回到此视图时,网格都不会更新。我知道我需要调用gred.getDatapRovider()。refreshall();在我的代码中的某个地方以获取这些更新的网格,但我只是无法弄清楚在哪里说明它。我已经确认我正在正确地改变这些价值,但是这些变化并没有反映在网格中。

这是我的观点,可以访问我提到的网格:

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();
    }
}

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文