我的Ajax事件“eventSelect”无效。当我点击日历上的某个事件时,它会显示该对话框,但它是空的。当我在调试模式下运行我的应用程序时,参数selectEvent为null,而对于ajax事件“dateSelect”,这个工作正常(即使出现对话框,它也显示日期)。
appointment.xhtml
<h:form>
<p:schedule id="schedule"
value="#{Appointment.eventModel}"
widgetVar="myschedule"
timeZone="GMT+2">
<p:ajax event="dateSelect"
listener="#{Appointment.onDateSelect}"
update="eventDetails"
oncomplete="PF('eventDialog').show();" />
<p:ajax event="eventSelect"
listener="#{Appointment.onEventSelect}"
update="eventDetails"
oncomplete="PF('eventDialog').show();" />
</p:schedule>
<p:dialog widgetVar="eventDialog"
header="Appointment Details"
showEffect="clip"
hideEffect="clip">
<h:panelGrid id="eventDetails" columns="2">
<p:outputLabel for="title" value="Titles:" />
<p:inputText id="title"
value="#{Appointment.event.title}"
required="true" />
<p:outputLabel for="from" value="From:" />
<p:calendar id="from"
value="#{Appointment.event.startDate}"
timeZone="GMT+2" pattern="dd/MM/yyyy HH:mm" />
<p:outputLabel for="to" value="To:" />
<p:calendar id="to"
value="#{Appointment.event.endDate}"
timeZone="GMT+2" pattern="dd/MM/yyyy HH:mm" />
<p:commandButton type="reset" value="Reset" />
<p:commandButton id="addButton" value="Save"
actionListener="#{Appointment.addEvent}"
oncomplete="PF('myschedule').update();
PF('eventDialog').hide();"/>
</h:panelGrid>
</p:dialog>
</h:form>
Appointment.java
public class Appointment implements AppointmentInterface {
public ScheduleModel getEventModel() {
eventModel = new DefaultScheduleModel();
return eventModel;
}
public void onDateSelect(SelectEvent selectEvent) {
event = new DefaultScheduleEvent("", (Date) selectEvent.getObject(), (Date) selectEvent.getObject());
}
public void onEventSelect(SelectEvent selectEvent) {
event = (ScheduleEvent) selectEvent.getObject();
}
public void addEvent(ActionEvent actionEvent) {
if (event.getId() == null) {
eventModel.addEvent(event);
} else {
eventModel.updateEvent(event);
}
event = new DefaultScheduleEvent();
}
}
答案 0 :(得分:1)
我遇到了同样的问题。 问题是关于创建eventModel:
public ScheduleModel getEventModel() {
eventModel = new DefaultScheduleModel();
return eventModel;
}
创建视图时将多次调用此代码,并且每次都会创建一个新的eventModel空实例。您应该在创建新实例
之前检查eventModel是否为nullpublic ScheduleModel getEventModel() {
if (eventModel==null) {
eventModel = new DefaultScheduleModel();
}
return eventModel;
}
因此,eventModel的实例只会创建一次,而schedule将使用此实例。