jsf 2 - Calling a Bean Function in a JavaScript if statement for eveluating Primefaces object - Stack Overflow

I am using Primefaces Schedule and I am usingwhich has a listener parameter of org.primefaces.event.D

I am using Primefaces Schedule and I am using which has a listener parameter of org.primefaces.event.DateSelectEvent. Based on whether DateSelectEvent.getDate is a weekeday or a weekend, I need to open a dialog box or just disable ajax (or don't do anything). For that I need to make a decision in the onpelete attribute of . I have gone as far as this:

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

Well, obviously onDateSelect.getDate == Weekday doesn't work and must be taken care of by a function on my backing bean, but how can I evaluate my backing bean method in JS function?

I am using Primefaces Schedule and I am using which has a listener parameter of org.primefaces.event.DateSelectEvent. Based on whether DateSelectEvent.getDate is a weekeday or a weekend, I need to open a dialog box or just disable ajax (or don't do anything). For that I need to make a decision in the onpelete attribute of . I have gone as far as this:

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

Well, obviously onDateSelect.getDate == Weekday doesn't work and must be taken care of by a function on my backing bean, but how can I evaluate my backing bean method in JS function?

Share Improve this question edited Feb 6, 2013 at 15:14 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Feb 6, 2013 at 6:33 Saed AlaviniaSaed Alavinia 2343 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

The onplete attribute of PrimeFaces ponents doesn't support evaluating requestbased EL expressions. One of the ways is to use RequestContext#execute() inside onDateSelect listener method instead of a onplete.

RequestContext requestContext = RequestContext.getCurrentInstance();

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

Another way is to ajax-update a <h:outputScript> block containing the desired 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>

As per the requirement you've mentioned I believe you are looking for something with which you can use a Javascript function and invoke your backing bean method to evaluate if its a weekday.

You can use Ajax4jsf's a4j:jsFunction like:

<p:ajax event="dateSelect" 
        listener="#{myBean.onDateSelect}" 
        onplete="checkWeekDay"/>

<a4j:jsFunction name="checkWeekDay" action="#{beanName.methodName}"/>

You can use EL in the javascript mands too. So I assume you have method named isWeekDay similar to folowing, or just use your own function to determine whether the date is a week day 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}" onplete="
       if (#{yourBean.weekDay()}) { 
           eventDialog.show()
       } else {
           myschedule.update()
       }"
>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745451031a4628268.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信