java 如何使用 com.google.gwt.user.datepicker.client.DateBox 限制可用的日期范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5656495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to limit the available Date ranges with the com.google.gwt.user.datepicker.client.DateBox
提问by
I need to limit what Dates
a user can pick from the com.google.gwt.user.datepicker.client.DateBox
.
我需要限制Dates
用户可以从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.
我似乎无法弄清楚如何限制 minDate
以便他们无法选择过去的日期。
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?
如果我不能这样做,com.google.gwt.user.datepicker.client.DateBox
是否有替代DateBox
小部件可以让我具有这种灵活性?
回答by
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
根据我收到的建议,这是我想出的将可选日期限制为当天和之后的方法。这适用于 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:
为了完整起见,这里是static
操作日期的辅助方法:
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));
}
回答by dafunkeemonkee
I am guessing here but you may need to use the addValueChangeHandler. An example:
我在这里猜测,但您可能需要使用 addValueChangeHandler。一个例子:
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).
在这里,当用户以某种方式选择过去的日期时,它会自动将其设置为当前日期(或任何适合您需要的日期)。