I have two fields in my website. An inputtext and a selectManyCheckbox. In the inputtext the user writes a date like 30.03.2014. The selectManyCheckbox has all days of a week (monday, tuesday…). I try to achieve that if the user writes in a date, the day of the week for this date gets checked in the selectManyCheckbox. For example 30.03.2014 should check sunday.
Here is some code of my jsp:
<h:inputText value="#{course.startTime}" onblur="submit()" valueChangeListener="#{course.intervalDayEvent}" immediate="true">
<f:converter converterId="dateConverter" />
</h:inputText>
<h:selectManyCheckbox layout="pageDirection" value="#{course.days}">
<f:selectItems value="#{course.availableDays}" />
</h:selectManyCheckbox>
And here is some code of the managedBean:
private List<String> days = new ArrayList<String>();
private List<SelectItem> availableDays = new ArrayList<SelectItem>();
@PostConstruct
public void init() {
availableDays.add(new SelectItem("Montag", "Montag"));
availableDays.add(new SelectItem("Dienstag", "Dienstag"));
availableDays.add(new SelectItem("Mittwoch", "Mittwoch"));
availableDays.add(new SelectItem("Donnerstag", "Donnerstag"));
availableDays.add(new SelectItem("Freitag", "Freitag"));
availableDays.add(new SelectItem("Samstag", "Samstag"));
availableDays.add(new SelectItem("Sonntag", "Sonntag"));
days.add("Montag");
}
public void intervalDayEvent(ValueChangeEvent event) {
try {
Date date = (Date) event.getNewValue();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_WEEK);
days.clear();
switch (day) {
case Calendar.MONDAY:
days.add("Montag");
break;
case Calendar.TUESDAY:
days.add("Dienstag");
break;
case Calendar.WEDNESDAY:
days.add("Mittwoch");
break;
case Calendar.THURSDAY:
days.add("Donnerstag");
break;
case Calendar.FRIDAY:
days.add("Freitag");
break;
case Calendar.SATURDAY:
days.add("Samstag");
break;
case Calendar.SUNDAY:
availableDays.get(6).setLabel("Test");
days.add("Sonntag");
break;
}
} catch (Exception ex) {
log.warn(ex.getMessage());
}
FacesContext context = FacesContext.getCurrentInstance();
context.renderResponse();
}
As you can see monday (Montag) is prechecked. If I type 30.03.2014 into the inputtext, the label of sunday changes to “Test”. But the checked item doesn’t change from monday to sunday.
I hope you can help me with this. 🙂
Benjamin
I presume the input text and the selectManyCheckbox are in the same form? I can’t see what the JS function submit() is doing but I gather it is just submitting the whole form. What is happening is the intervalDayEvent is being triggered but then because the form has been submitted the selectManyCheckbox is then setting the value of its component in the backing bean.
To get around this you need to have these components in 2 different forms or use some ajax controls that will only set the input text when that value changes and not the whole form. An example of this can be shown below with RichFaces but am sure you can do it with other JSF ajax libraries: