0

I am using a function to disable all days excepts mondays or enable the next working day to the holiday monday. I run the function before show the calendar and it works perfectly but I need to run the function again when the user change the current month in the calendar.

This is the calendar:

<p:calendar beforeShowDay="disableDays" navigator="true" readonlyInput="true" showOn="button" value="#{bean.date}">
    <f:convertDateTime pattern="yyyy/MM/dd" timeZone="#{timeZone}" />
</p:calendar>

And the function:

function disableDays(date) {
    var datesToDisable = [ "8-1-2015", "8-2-2015", "8-4-2015", "8-5-2015", "8-6-2015", "8-7-2015", "8-8-2015", "8-9-2015", "8-11-2015", "8-12-2015", "8-13-2015", "8-14-2015", "8-15-2015", "8-16-2015", "8-17-2015", "8-19-2015", "8-20-2015", "8-21-2015", "8-22-2015", "8-23-2015", "8-25-2015", "8-26-2015", "8-27-2015", "8-28-2015", "8-29-2015", "8-30-2015" ];
    var month = date.getMonth(), day = date.getDate(), annio = date.getFullYear();
    for (i = 0; i < datesToDisable.length; i++) {
        if ($.inArray((month + 1) + '-' + day + '-' + annio, datesToDisable) != -1) {
            return [ false ];
        }
    }
    return [ true ];
}

How can I do that?

enter image description here

John Alexander Betts
  • 4,718
  • 8
  • 47
  • 72
  • Put the disableDays javascript function within a block that you update via ajax when a month change event happens on the Calendar. Then use EL within the javascript function that puts the new days to disable in there. See point 1 in http://stackoverflow.com/questions/2547814/mixing-jsf-el-in-a-javascript-file – Kukeltje Aug 11 '15 at 19:04
  • what is the change month event? – John Alexander Betts Aug 11 '15 at 19:13
  • Sorry it is called the viewChange event what it is can be read in the PF docs – Kukeltje Aug 11 '15 at 19:34
  • It doesn't appear in the pdf docs, thanks anyway – John Alexander Betts Aug 12 '15 at 12:51
  • Sorry, (and please take a little offence) then do not know how to use ctrl-f. It is on page 55 of the 5.2 docs... wow... – Kukeltje Aug 12 '15 at 12:59
  • Yes, you are right, but I don't find how to use it and I don't find an example nowhere – John Alexander Betts Aug 12 '15 at 15:38
  • It is to be used as any other event that you can use with ajax. I hope that much is clear. And how to use p:ajax can be found on > 1.000 sites on the internet. – Kukeltje Aug 12 '15 at 16:11
  • Excuse me, because my native language is spanish sometimes is hard to me to explain what is my problem. I know how to use ajax calls in primefaces. The problem is disabling the days in the calendar for other monts when the user change the date – John Alexander Betts Aug 12 '15 at 16:22
  • English not being your native language is not really a problem I think, since it is not to bad. But if you doubt that, use Google translate. Now on topic: You know how to disable dates, you know how to use ajax, I gave a link to a page where you can see how to use EL in javascript, and also told that you should put the disableDays function in a block that you update via Ajax. You should be able to combine that, or at least give it a try and post where you get so we can comment on that. That is what you don't do and that has nothing to do with English not being your native language. – Kukeltje Aug 12 '15 at 16:33

1 Answers1

1

This answer is a summary in partly pseudo code when all the comments on the question would be interpreted and worked with. It could contain typos, wrong classes but the intention is what counts now. Maybe I'll improve it later

xhtml:

<p:calendar...>
    <p:ajax event="viewChange" listener="#{bean.myViewAction}" update="scriptPanel" />
</p:calendar>

<h:panelGroup id="scriptPanel">
    <script>
        function disableDays(date) {
            var datesToDisable = #{bean.disableDates};
            var month = date.getMonth(), day = date.getDate(), annio = date.getFullYear();
            for (i = 0; i < datesToDisable.length; i++) {
                if ($.inArray((month + 1) + '-' + day + '-' + annio, datesToDisable) != -1) {
                    return [ false ];
                }
            }
            return [ true ];
        }
    </script>
</h:panelGroup>

Bean:

public void viewChange(DateViewChangeEvent e) { 
    // Get relvant info from event
    // determine new disableDates and set those in the correct formatted string
}

public void getDisableDates() {
    return disableDates;
}
Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Please edit the answer (I'll 'accept' the changes) to what you realy have and create a new question – Kukeltje Aug 12 '15 at 19:46
  • :-D I meant you to improve the answer so it contains more real code. But this is fine to. Next time, you should experiment more based on comments. This is all not to difficult – Kukeltje Aug 12 '15 at 20:29
  • You can see the new question here: http://stackoverflow.com/questions/31998536/disable-days-in-the-other-calendar-when-month-is-changed If you can help me – John Alexander Betts Aug 13 '15 at 21:14