2

Is it possible to do a conditional update with Ajax. I have two calendar components and the code is as follows

Calendar component 1 (workDate)

<h:outputLabel value="#{lbl.WorkDate}" for="workDate" rendered="#{!userManager.customerUser}"/>
<p:calendar id="workDate" value="#{jobs_Builder.selected.workDate}" pattern="dd/MM/yyyy" mask="true" rendered="#{!userManager.customerUser}" mindate="Date()">
      <p:ajax event="dateSelect" update="requestedDeliveryDate" />
</p:calendar>

Calendar component 2(requestedDeliveryDate)

<h:outputLabel value="#{lbl.RequestedDeliveryDate}" for="requestedDeliveryDate" rendered="#{!userManager.customerUser}"/>
<p:calendar id="requestedDeliveryDate" value="#{jobs_Builder.selected.requestedDeliveryDate}" pattern="dd/MM/yyyy" mask="true" rendered="#{!userManager.customerUser}" mindate="#{jobs_Builder.selected.workDate}"/>

The behavior is whenever the date is set in wordkdate, the requestedDeliveryDate is reset, but what I want is to get that reset only if the requestedDeliveryDate is earlier than the workDate.

Is this something which I can do?

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
rnavagamuwa
  • 310
  • 2
  • 11
  • Have a look at https://stackoverflow.com/questions/16378099/make-multiple-dependent-cascading-selection-components-in-jsf This is the same principle. – Jasper de Vries Nov 06 '20 at 12:29
  • 2
    Yes in your `dateSelect` event don't use `update` call a Java `listener` method and in that method you can check the dates and if the date meets your requirement then you can choose to update the other calendar from the server side with `PrimeFaces.current().ajax().update("requestedDeliveryDate");` – Melloware Nov 06 '20 at 12:48
  • Thanks a lot, @Melloware. Works like a charm – rnavagamuwa Nov 06 '20 at 21:01
  • OK I posted it as the answer if you don't mind selecting it as the solution – Melloware Nov 07 '20 at 13:33

1 Answers1

0
  1. Change your dateSelect event not to update anything but use a Java listener instead like..
<p:ajax event="dateSelect" listener="#{controller.onDateSelect}" />
  1. In your Java method check for your two dates and if your condition is met update the UI component from this method.
public void onDateSelect() {
    if (date1 > date2) {
        // update other calendar
        PrimeFaces.current().ajax().update("requestedDeliveryDate");
    }
}
Melloware
  • 10,435
  • 2
  • 32
  • 62