How to limit the available Date ranges with the com.google.gwt.user.datepicker.client.DateBox
Asked Answered
N

2

8

I need to limit what Dates a user can pick from the com.google.gwt.user.datepicker.client.DateBox.

I can't seem to figure out how to limit the min Date so they can't pick past dates.

If I can't do this with com.google.gwt.user.datepicker.client.DateBox is there an alternative DateBox widget that will allow me this kind of flexibility?

Natelson answered 13/4, 2011 at 22:36 Comment(0)
N
18

Based on the suggestions I received, here is what I came up with that works on limiting the selectable dates to only the current day and after. This works on GWT 2.1.1

final DateBox dateBox = new DateBox();
dateBox.addValueChangeHandler(new ValueChangeHandler<Date>()
{
    @Override
    public void onValueChange(final ValueChangeEvent<Date> dateValueChangeEvent)
    {
        if (dateValueChangeEvent.getValue().before(today()))
        {
            dateBox.setValue(today(), false);
        }
    }
});
dateBox.getDatePicker().addShowRangeHandler(new ShowRangeHandler<Date>()
{
    @Override
    public void onShowRange(final ShowRangeEvent<Date> dateShowRangeEvent)
    {
        final Date today = today();
        Date d = zeroTime(dateShowRangeEvent.getStart());
        while (d.before(today))
        {
            dateBox.getDatePicker().setTransientEnabledOnDates(false, d);
            d = nextDay(d);
        }
    }
});

And for completeness here are the static helper methods for manipulating the dates:

private static Date today()
{
    return zeroTime(new Date());
}

/** this is important to get rid of the time portion, including ms */
private static Date zeroTime(final Date date)
{
    return DateTimeFormat.getFormat("yyyyMMdd").parse(DateTimeFormat.getFormat("yyyyMMdd").format(date));
}

private static Date nextDay(final Date date)
{
    return zeroTime(new Date(date.getTime() + 24 * 60 * 60 * 1000));
}
Natelson answered 14/4, 2011 at 17:29 Comment(3)
Great answer, but I'd like to address a small bug in the onShowRangeHandler method. The loop should also be bounded to the end range (otherwise throws silent dev-environment assertion exceptions) ` final long endTime = event.getEnd().getTime(); while (d.before(today) && d.getTime() <= endTime) { dateBox.getDatePicker().setTransientEnabledOnDates(false, d); d = nextDay(d); } `Fusain
Also, during a transition from Daylight Time to Standard Time, this code will enter into an infinite loopReplication
Please use com.google.gwt.user.datepicker.client.CalendarUtil in order to add one day to the current date: CalendarUtil.addDaysToDate (d, 1);. This will also work for changes from daylight saving to standard time and vice versa. And you do not have to cope with the time parts!Stipendiary
P
1

I am guessing here but you may need to use the addValueChangeHandler. An example:

datebox.addValueChangeHandler(new ValueChangeHandler<Date>() {
    public void onValueChange(ValueChangeEvent<Date> event) {
        Date date = event.getValue();
        if (date.before(new Date())) {
         fromDatePicker.setValue(new Date());
        }
    }
});

Here when a user somehow selects a date that is in the past, it will auto set it to current date (or whatever fits your needs).

Polynesia answered 13/4, 2011 at 22:53 Comment(2)
I would rather just disable any dates in the past and avoid the possibility of them picking one at all. Is there a way to disable the actual clickable dates in the calendar widget?Natelson
this is on the right track, but it is inherently flawed. Since new Date() has time associated with it, the date.before(new Date()) will always fail. I had to strip off the time to get this approach to work. Now I can reset the date to today, but am still looking for a way to disable the past dates completely.Natelson

© 2022 - 2024 — McMap. All rights reserved.