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

一个人想着一个人 提交于 2019-12-30 04:44:06

问题


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?


回答1:


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));
}



回答2:


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).



来源:https://stackoverflow.com/questions/5656495/how-to-limit-the-available-date-ranges-with-the-com-google-gwt-user-datepicker-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!