Call the Bean function in a JavaScript if statement to define a Primefaces object

I am using Primefaces Schedule and I am using one that has listener parameter org.primefaces.event.DateSelectEvent . Based on whether DateSelectEvent.getDate is weekly or weekend, I need to open a dialog box or just disable ajax (or do nothing). To do this, I need to make a decision in the oncompelete attribute. I got to this:

 <p:ajax event="dateSelect" listener="#{myBean.onDateSelect}" oncomplete=" if (onDateSelect.getDate == WeekDay) { eventDialog.show() } else { myschedule.update() }" > 

Well, it is obvious that onDateSelect.getDate == Weekday does not work and should take care of the function on my bean support, but how can I evaluate my bean support method in the JS function?

+4
source share
3 answers

The oncomplete attribute of oncomplete components does not support the evaluation of query-based EL expressions. One way is to use RequestContext#execute() inside the onDateSelect the listener method instead of oncomplete .

 RequestContext requestContext = RequestContext.getCurrentInstance(); if (isWeekDay(onDateSelect.getDate())) { requestContext.execute("eventDialog.show()"); } else { requestContext.execute("myschedule.update()"); } 

Another way is ajax-update a <h:outputScript> block containing the required EL expressions.

 <p:ajax event="dateSelect" listener="#{myBean.onDateSelect}" update="script" /> ... <h:panelGroup id="script"> <h:outputScript> if (#{myBean.weekDaySelected}) { eventDialog.show() } else { myschedule.update() } </h:outputScript> </h:panelGroup> 
+2
source

According to your requirement, I believe that you are looking for something with which you can use the Javascript function and call your bean method support to evaluate if there is a day of the week.

You can use Ajax4jsf a4j:jsFunction as:

 <p:ajax event="dateSelect" listener="#{myBean.onDateSelect}" oncomplete="checkWeekDay"/> <a4j:jsFunction name="checkWeekDay" action="#{beanName.methodName}"/> 
0
source

You can also use EL in javascript commands. Therefore, I assume that you have a method called isWeekDay similar to folowing, or just use your own function to determine if the date is a weekday or not:

 public boolean isWeekDay() { Calendar c = Calendar.getInstance(); c.setTime(yourDate); return c.get(Calendar.DAY_OF_WEEK) < 5; } <p:ajax event="dateSelect" listener="#{myBean.onDateSelect}" oncomplete=" if (#{yourBean.weekDay()}) { eventDialog.show() } else { myschedule.update() }" > 
0
source

All Articles