如何使用 com.google.gwt.user.datepicker.client.DateBox 限制可用的日期范围 [英] How to limit the available Date ranges with the com.google.gwt.user.datepicker.client.DateBox
问题描述
我需要限制用户可以从 com.google.gwt.user.datepicker.client.DateBox
中选择的 Dates
.
I need to limit what Dates
a user can pick from the com.google.gwt.user.datepicker.client.DateBox
.
我似乎不知道如何限制最小 Date
以便他们无法选择过去的日期.
I can't seem to figure out how to limit the min Date
so they can't pick past dates.
如果我不能用 com.google.gwt.user.datepicker.client.DateBox
做到这一点,是否有替代的 DateBox
小部件可以让我使用这种方式灵活性?
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?
推荐答案
根据我收到的建议,这里是我想出的将可选日期限制为当天和之后的方案.这适用于 GWT 2.1.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);
}
}
});
为了完整起见,这里是用于操作日期的 static
辅助方法:
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));
}
这篇关于如何使用 com.google.gwt.user.datepicker.client.DateBox 限制可用的日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!