Reputation: 4103
I want to disable a specific date in my date box. I tried it with this code, but the specified date (current day) does not get disabled.
final DateBox dateBox = new DateBox();
dateBox.getDatePicker().addShowRangeHandler(new ShowRangeHandler<Date>() {
@Override
public void onShowRange(final ShowRangeEvent<Date> event) {
List<Date> disabledDates = new ArrayList<Date>();
disabledDates.add(new Date());
dateBox.getDatePicker().setTransientEnabledOnDates(false, disabledDates);
}
});
Is there an other way to do this?
Edit: Following the example of apanizo, the day 29.5 looks greyed out, but is clickable nevertheless.
Upvotes: 0
Views: 2482
Reputation: 628
Really sorry, I did not test the code of my last answer.
I have just tried and your code worked to me, the only thing that I did was pass all the dates that I do not want through the setTransientEnabledOnDates(false, dateToDisable);
method.
Example:
public void onModuleLoad() {
final DateBox dateBox = new DateBox();
dateBox.getDatePicker().addShowRangeHandler(new ShowRangeHandler<Date>() {
@Override
public void onShowRange(final ShowRangeEvent<Date> dateShowRangeEvent) {
final Date today = new Date(); //30 May 3014
final Date yesterday = new Date(today.getTime()- 24*60*60*1000);
//disabling yesterday - 29 May of 2014
dateBox.getDatePicker().setTransientEnabledOnDates(false, yesterday);
}
});
RootPanel.get().add(dateBox);
}
If you want to disable the click event on a disabled date, this is a resolved and not delivered bug.
See: https://code.google.com/p/google-web-toolkit/issues/detail?id=7876
I think in the GWT's 2.7 version it will be released, in the mean time you can patch it introducing in the line 77 of com.google.gwt.user.datepicker.client.CellGridImpl
:
addDomHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if (isActive(Cell.this)) {
setSelected(Cell.this);
}
}
}, ClickEvent.getType());
In detail here
Upvotes: 1
Reputation: 46841
Simply add a value changed listener on date picker to listen for any change in the date.
For example if current date is not allowed and it's selected then simply discard the changes and revert back to previous date or show a warning message as well if needed and add style for current date to look it as disabled.
Here date time format is used to check for date only, time is discarded while checking dates.
Sample code:
private Date prevDate;
...
final DateBox dateBox = new DateBox();
final DateTimeFormat dateTimeFormat=DateTimeFormat.getFormat("MM/dd/yyyy");
dateBox.getDatePicker().addValueChangeHandler(new ValueChangeHandler<Date>() {
@Override
public void onValueChange(ValueChangeEvent<Date> event) {
if (dateTimeFormat.format(event.getValue()).equals(dateTimeFormat.format(new Date()))) {
dateBox.setValue(prevDate);
// show warning message here
}else{
prevDate=event.getValue();
}
}
});
Upvotes: 0