Reputation: 29
i have the following code in an .xhtml file
<!-- /row -->
<div class="row" id="penaltyStartingDate" style="margin-top: 15px">
<div class="span8">
<div class="control-group">
<label class="hcg-control-label span5">Ημερομηνία Έναρξης Ισχύος Ποινής</label>
<div class="controls span7">
<p:calendar id="penaltyStartingDate" styleClass="hcg-full-width cursor-pointer" pattern="dd/MM/yyyy"
value="#{penaltiesView.penalty.penaltyStartingDate}" />
<i class="fa fa-calendar hcg-input-icon"></i>
</div>
</div>
</div>
<!-- /span -->
</div>
<!-- /row -->
<div id="forfeitureDurationContainer" class="row" style="margin-top: 15px">
<div class="span8">
<div class="control-group">
<label class="hcg-control-label span5">Χρόνος Στέρησης</label>
<div class="controls span7">
<p:inputText id="ForfeitureDurationValue" styleClass="hcg-full-width" value="#{penaltiesView.penalty.forfeitureDurationValue}" disabled="#{!penaltiesView.canEdit()}" style="width:30%" required="false" requiredMessage="Ο χρόνος στέρησης είναι υποχρεωτικός." />
<p:selectOneMenu id="ForfeitureDurationType" value="#{penaltiesView.penalty.forfeitureDurationType}" disabled="#{!penaltiesView.canEdit()}" styleClass="margin-bottom-10" style="width:70%">
<f:selectItem itemLabel="Μήνες" itemValue="Months" />
<f:selectItem itemLabel="Ημέρες" itemValue="Days" />
<f:selectItem itemLabel="Χρόνια" itemValue="Years" />
</p:selectOneMenu>
</div>
</div>
</div>
<!-- /span -->
</div>
<!-- /row -->
<div class="row" id="penaltyStopDate" style="margin-top: 15px">
<div class="span8">
<div class="control-group">
<label class="hcg-control-label span5">Ημερομηνία Παύσης Ισχύος Ποινής</label>
<div class="controls span7">
<p:calendar id="penaltyStopDate" styleClass="hcg-full-width cursor-pointer" pattern="dd/MM/yyyy" disabled="true"
value="#{penaltiesView.penalty.penaltyStopDate}" />
<i class="fa fa-calendar hcg-input-icon"></i>
</div>
</div>
</div>
<!-- /span -->
</div>
And this is what it generates in the webpage screenshot
The first field is a primefaces calendar. The next one is a field where you put a number and from what you choose in the dropdown menu (which includes days, months, years), a calculated date must appear in the final field which i have disabled. So for example, if I add 01/06/2020 and then type 2 and choose the "days(Ημέρες)" option, in the final field must appear the date 03/06/2020.
Can you help me with this one? I would really appreciate it.
Upvotes: 0
Views: 702
Reputation: 127
you can achieve it with Calendar. Add ajax in selectOneMenu
:
<p:selectOneMenu id="ForfeitureDurationType" value="#{penaltiesView.penalty.forfeitureDurationType}"
disabled="#{!penaltiesView.canEdit()}" styleClass="margin-bottom-10" style="width:70%">
<p:ajax event="change" listener="#{penaltiesView.calculatePenalty()}"
process="penaltyStartingDate,ForfeitureDurationValue,ForfeitureDurationType"/>
<f:selectItem itemLabel="Μήνες" itemValue="Months" />
<f:selectItem itemLabel="Ημέρες" itemValue="Days" />
<f:selectItem itemLabel="Χρόνια" itemValue="Years" />
</p:selectOneMenu>
Then in penaltiesView:
public void calculatePenalty() {
Calendar c = Calendar.getInstance();
c.setTime(penalty.penaltyStartingDate);
if (penalty.forfeitureDurationType.equals("Months") {
c.add(Calendar.MONTH, penalty.forfeitureDurationValue);
penalty.penaltyStopDate = c.getTime();
} else if (penalty.forfeitureDurationType.equals("Days") {
c.add(Calendar.DAY_OF_MONTH, penalty.forfeitureDurationValue);
penalty.penaltyStopDate = c.getTime();
} else {
c.add(Calendar.YEAR, penalty.forfeitureDurationValue);
penalty.penaltyStopDate = c.getTime();
}
PrimeFaces.current().ajax().update("penaltyStopDate");
}
Upvotes: 2