javascript 使用javascript在primefaces日历上动态设置默认日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15902004/
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
Dynamically set default date on primefaces calendar with javascript
提问by Magnilex
I need to set a suggested date for a p:calendar
given the selected date in another calendar. I can get a string representation by the date to set by parsing the DOM, so no problems there. I try to use the Client Side APIfor the component, because the preferred way is to do this as the calendar is selected:
我需要为p:calendar
另一个日历中的给定选定日期设置建议日期。我可以通过解析 DOM 来设置要设置的日期的字符串表示,所以没有问题。我尝试对组件使用客户端 API,因为首选方法是在选择日历时执行此操作:
The Client Side API method(as described in the primefaces documentation):
客户端 API 方法(如 primefaces 文档中所述):
setDate(date):Date to display (Sets display date)
setDate(date):显示日期(设置显示日期)
The calendar component:
日历组件:
<p:calendar value="#{someVar.startDate}"
locale="en_GB"
navigator="true"
id="startDate"
showOtherMonths="true"
size="11"
pattern="yyyy-MM-dd"
pages="4"
widgetVar="startDateCalendar"
onclick="startDateCalendar.setDate(new Date(2012, 9, 9));"/>
This does not affect anything, even though the function is being executed. I have debugged that.
即使正在执行函数,这也不会影响任何事情。我已经调试过了。
Is it possible to use javascript to set a default date for p:calendar
? Ideally, it should work as the pagedate
attribute for the component, i.e. it is only a suggested date pre-filled for the calendar, not pre-fill an actual value.
是否可以使用 javascript 设置默认日期p:calendar
?理想情况下,它应该用作pagedate
组件的属性,即它只是为日历预填充的建议日期,而不是预填充实际值。
回答by Nina
Set the default date in your bean.
在 bean 中设置默认日期。
private Date date = new Date(); // define your start date here
public Date getDate() {
return date;
}
public Date setDate(final Date newDate) {
this.date = newDate;
}
回答by Pawe? Albecki
Try onclick="PF('calendar_widget_name').setDate(new Date(2012, 9, 9));"
尝试 onclick="PF('calendar_widget_name').setDate(new Date(2012, 9, 9));"