You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Primefaces:如何在<p:schedule>中使用DefaultScheduleEvent对象

处理PrimeFaces <p:schedule>与自定义事件数据的方案

嘿,我来帮你搞定这个日程组件的问题!想要给<p:schedule>添加额外数据并在弹窗里展示、保存,核心思路是扩展DefaultScheduleEvent类,把你需要的自定义字段加进去,这样就能全程携带这些数据了。下面一步步来:

1. 自定义ScheduleEvent子类

DefaultScheduleEvent只提供了基础的title、start、end,我们自己写个类继承它,添加你需要的额外字段(比如描述、地点、关联ID等等),记得加上getter和setter:

import org.primefaces.model.DefaultScheduleEvent;
import java.util.Date;

public class CustomScheduleEvent extends DefaultScheduleEvent {
    // 自定义字段示例:描述、地点、关联用户ID
    private String description;
    private String location;
    private Long userId;

    // 复用父类构造方法,也可以根据需求扩展
    public CustomScheduleEvent(String title, Date start, Date end) {
        super(title, start, end);
    }

    // Getter和Setter方法
    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }
    public String getLocation() { return location; }
    public void setLocation(String location) { this.location = location; }
    public Long getUserId() { return userId; }
    public void setUserId(Long userId) { this.userId = userId; }
}

2. 用自定义事件填充日历

原来的循环逻辑里,把创建DefaultScheduleEvent改成创建CustomScheduleEvent,同时设置额外字段:

@ManagedBean
@ViewScoped
public class ScheduleBean {
    private List<CustomScheduleEvent> events;

    @PostConstruct
    public void init() {
        events = new ArrayList<>();
        // 模拟循环填充数据(替换成你实际的数据源逻辑)
        for (int i = 0; i < 5; i++) {
            Date start = // 你的起始时间逻辑;
            Date end = // 你的结束时间逻辑;
            CustomScheduleEvent event = new CustomScheduleEvent("项目会议" + i, start, end);
            // 设置自定义字段
            event.setDescription("讨论项目" + i + "的进度与风险");
            event.setLocation("总部3楼会议室" + (i+1));
            event.setUserId(100L + i);
            events.add(event);
        }
    }

    // Getter方法
    public List<CustomScheduleEvent> getEvents() { return events; }
}

对应的xhtml里,<p:schedule>的value绑定到这个自定义事件列表:

<p:schedule id="schedule" value="#{scheduleBean.events}" 
            eventSelectListener="#{scheduleBean.onEventSelect}"
            widgetVar="scheduleWidget"
            oncomplete="PF('eventDialogWidget').show()">
</p:schedule>

3. 在<p:dialog>中展示额外信息

当用户点击日程事件时,通过eventSelectListener获取选中的自定义事件,然后在弹窗里展示所有字段:

首先在后台Bean里添加选中事件的属性和监听方法:

private CustomScheduleEvent selectedEvent;

public void onEventSelect(ScheduleEntrySelectEvent event) {
    // 把选中的事件强转成我们的自定义类
    this.selectedEvent = (CustomScheduleEvent) event.getScheduleEvent();
}

// Getter方法
public CustomScheduleEvent getSelectedEvent() { return selectedEvent; }

然后添加弹窗的xhtml代码:

<p:dialog id="eventDialog" header="事件详情" widgetVar="eventDialogWidget" 
          modal="true" closable="true" width="400">
    <h:panelGrid columns="2" cellpadding="5" styleClass="ui-grid-responsive">
        <h:outputText value="标题:" styleClass="ui-outputlabel-bold" />
        <h:outputText value="#{scheduleBean.selectedEvent.title}" />
        
        <h:outputText value="开始时间:" styleClass="ui-outputlabel-bold" />
        <h:outputText value="#{scheduleBean.selectedEvent.start}" >
            <f:convertDateTime pattern="yyyy-MM-dd HH:mm" />
        </h:outputText>
        
        <h:outputText value="结束时间:" styleClass="ui-outputlabel-bold" />
        <h:outputText value="#{scheduleBean.selectedEvent.end}" >
            <f:convertDateTime pattern="yyyy-MM-dd HH:mm" />
        </h:outputText>
        
        <!-- 展示自定义字段 -->
        <h:outputText value="事件描述:" styleClass="ui-outputlabel-bold" />
        <h:outputText value="#{scheduleBean.selectedEvent.description}" />
        
        <h:outputText value="举办地点:" styleClass="ui-outputlabel-bold" />
        <h:outputText value="#{scheduleBean.selectedEvent.location}" />
    </h:panelGrid>
    
    <!-- 如果需要编辑保存,可以添加输入框和保存按钮 -->
    <p:commandButton value="保存修改" action="#{scheduleBean.saveEvent}" 
                     update="schedule" oncomplete="PF('eventDialogWidget').hide()" 
                     styleClass="ui-priority-primary" />
</p:dialog>

4. 保存额外数据

在后台的saveEvent方法里,直接获取selectedEvent的所有字段(包括自定义的),然后执行保存逻辑(比如存到数据库):

public void saveEvent() {
    // 这里可以拿到所有字段:title、start、end、description、location等
    String eventTitle = selectedEvent.getTitle();
    String eventDesc = selectedEvent.getDescription();
    String eventLocation = selectedEvent.getLocation();
    
    // 执行保存操作,比如调用DAO层持久化
    // eventRepository.save(selectedEvent);
}

这样整个流程就通了:自定义事件携带额外数据→填充到日历→点击事件弹窗展示所有数据→保存时一并处理自定义字段。

内容的提问来源于stack exchange,提问作者Mauricio Morales Osorio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:49:33