Metadata for the dates in the date picker calendar. Allows you to lazily change attributes like
disabled
and styleClass
for dates when the calendar view changes.
<style>
.start { background: #81c784; }
.deadline { background: #ef9a9a; }
.ui-datepicker-calendar .ui-state-disabled { opacity: .2; }
</style>
<p:growl id="growl"/>
<div class="card">
<h:form id="form">
<h:panelGrid columns="2" cellpadding="7">
<p:outputLabel for="basic" value="Basic"/>
<p:datePicker id="basic" value="#{datePickerMetadataView.date1}"
model="#{datePickerMetadataView.model}"/>
<p:outputLabel for="lazy" value="Lazy and Navigator"/>
<p:datePicker pattern="yyyy-MM-dd" id="lazy" value="#{datePickerMetadataView.date2}"
model="#{datePickerMetadataView.modelLazy}"
monthNavigator="true" yearNavigator="true"/>
<p:outputLabel for="lazyEnabled" value="Lazy and Navigator - Enabled dates"/>
<p:datePicker pattern="yyyy-MM-dd" id="lazyEnabled" value="#{datePickerMetadataView.date5}"
model="#{datePickerMetadataView.modelEnabledDaysLazy}"
monthNavigator="true" yearNavigator="true"/>
<p:outputLabel for="basicEvent" value="Basic and Event"/>
<p:datePicker id="basicEvent" value="#{datePickerMetadataView.date3}"
model="#{datePickerMetadataView.model}">
<p:ajax event="viewChange" listener="#{datePickerMetadataView.onViewChange}" update="growl"/>
</p:datePicker>
<p:outputLabel for="lazyEvent" value="Lazy and Event"/>
<p:datePicker inline="true" id="lazyEvent" value="#{datePickerMetadataView.date4}"
model="#{datePickerMetadataView.modelLazy}">
<p:ajax event="viewChange" listener="#{datePickerMetadataView.onViewChange}" update="growl"/>
</p:datePicker>
<p:outputLabel for="lazyEnabledEvent" value="Lazy and Event - Enabled dates"/>
<p:datePicker pattern="yyyy-MM-dd" id="lazyEnabledEvent" value="#{datePickerMetadataView.date6}"
model="#{datePickerMetadataView.modelEnabledDaysLazy}">
<p:ajax event="viewChange" listener="#{datePickerMetadataView.onViewChange}" update="growl"/>
</p:datePicker>
</h:panelGrid>
</h:form>
</div>
package org.primefaces.showcase.view.input;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import java.io.Serializable;
import java.time.LocalDate;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.event.DateViewChangeEvent;
import org.primefaces.model.datepicker.DateMetadataModel;
import org.primefaces.model.datepicker.DefaultDateMetadata;
import org.primefaces.model.datepicker.DefaultDateMetadataModel;
import org.primefaces.model.datepicker.LazyDateMetadataModel;
@Named
@ViewScoped
@RegisterForReflection(serialization = true)
public class DatePickerMetadataView implements Serializable {
private final DateMetadataModel model;
private final DateMetadataModel modelLazy;
private final DateMetadataModel modelEnabledDaysLazy;
private LocalDate date1;
private LocalDate date2;
private LocalDate date3;
private LocalDate date4;
private LocalDate date5;
private LocalDate date6;
public DatePickerMetadataView() {
LocalDate start = LocalDate.now().withDayOfMonth(1);
DefaultDateMetadata metadataDisabled = DefaultDateMetadata.builder().disabled(true).build();
DefaultDateMetadata metadataStart = DefaultDateMetadata.builder().styleClass("start").build();
DefaultDateMetadata metadataDeadline = DefaultDateMetadata.builder().styleClass("deadline").build();
model = new DefaultDateMetadataModel();
model.add(start.minusMonths(1), metadataDisabled);
model.add(start.plusDays(start.getMonthValue() + 3), metadataStart);
model.add(start.plusDays(start.getMonthValue() + 6), metadataDisabled);
model.add(start.plusDays(start.getMonthValue() + 9), metadataDeadline);
model.add(start.plusMonths(1), metadataDisabled);
modelLazy = new LazyDateMetadataModel() {
@Override
public void loadDateMetadata(LocalDate start, LocalDate end) {
add(start.plusDays(start.getMonthValue() + 2), metadataStart);
add(start.plusDays(start.getMonthValue() + 5), metadataDisabled);
add(start.plusDays(start.getMonthValue() + 8), metadataDeadline);
}
};
// start and deadline have to be also enabled
DefaultDateMetadata metadataStartEnabled = DefaultDateMetadata.builder().styleClass("start").enabled(true).build();
DefaultDateMetadata metadataEnabled = DefaultDateMetadata.builder().enabled(true).build();
DefaultDateMetadata metadataDeadlineEnabled = DefaultDateMetadata.builder().styleClass("deadline").enabled(true).build();
modelEnabledDaysLazy = new LazyDateMetadataModel() {
@Override
public void loadDateMetadata(LocalDate start, LocalDate end) {
add(start.plusDays(start.getMonthValue() + 2), metadataStartEnabled);
// enable just five days since start
for (int i = 1; i < 5; i++) {
add(start.plusDays(start.getMonthValue() + 2 + i), metadataEnabled);
}
add(start.plusDays(start.getMonthValue() + 6), metadataDeadlineEnabled);
}
};
}
public void onViewChange(DateViewChangeEvent event) {
String summary = "Year: " + event.getYear() + ", month: " + event.getMonth();
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null);
FacesContext.getCurrentInstance().addMessage(null, message);
}
public LocalDate getDate1() {
return date1;
}
public void setDate1(LocalDate date1) {
this.date1 = date1;
}
public LocalDate getDate2() {
return date2;
}
public void setDate2(LocalDate date2) {
this.date2 = date2;
}
public LocalDate getDate3() {
return date3;
}
public void setDate3(LocalDate date3) {
this.date3 = date3;
}
public LocalDate getDate4() {
return date4;
}
public void setDate4(LocalDate date4) {
this.date4 = date4;
}
public LocalDate getDate5() {
return date5;
}
public void setDate5(LocalDate date5) {
this.date5 = date5;
}
public LocalDate getDate6() {
return date6;
}
public void setDate6(LocalDate date6) {
this.date6 = date6;
}
public DateMetadataModel getModel() {
return model;
}
public DateMetadataModel getModelLazy() {
return modelLazy;
}
public DateMetadataModel getModelEnabledDaysLazy() {
return modelEnabledDaysLazy;
}
}