【问题标题】:calender tag in jsf with date & time restrictionjsf中带有日期和时间限制的日历标签
【发布时间】:2017-09-22 13:06:57
【问题描述】:

我有一个名为 page.xhtml 的 jsf 页面,如下所示:

<p:calendar id="start_date_time_label" widgetVar="myCalendar1"
    value="#{bean.startDateTime}" pattern="dd/MM/yy"
    style="width: 359px; " required="true"
    requiredMessage="Start Date is mandatory" />

在这里,我添加了一个类似以下的 javascript 函数来限制最小日期值:

<script type="text/javascript">
        jQuery(document).ready(function(){
            myCalendar1.jqEl.datepicker("option", "minDate", -365);//set minDate to 1 year back 
        });     
</script>

到目前为止,脚本运行良好。

现在根据要求,我必须将 p:calender 标签的模式更改为 pattern="dd/MM/yy HH:mm:ss"。但随后脚本无法正常工作。请帮我让它与时间价值一起工作。

【问题讨论】:

  • 为什么不使用 JSF 验证器?见stackoverflow.com/questions/6047866/…
  • @JasperdeVries:因为使用 minDate 会阻止您在 UI 中选择它(但我更喜欢 PrimeFaces minDate 日历属性,那么 OP 可以在文档、展示、IDE 代码完成中找到和谷歌)

标签: javascript jsf primefaces


【解决方案1】:

为什么不使用 primefaces 的 mindate 属性?

<p:calendar id="start_date" mindate="#{of:addMonths(now, -12)}" value="#{bean.startDateTime}" pattern="dd/MM/yy HH:mm:ss" />

1 年前的最短日期是通过 omnifaces 的 now bean 及其 el date functions 的组合设置的,但您可以将其替换为返回相同内容的 bean 方法。

【讨论】:

  • 使用上述模式时是否有效?我的印象是它在没有时间的情况下使用了一种模式,但随着时间的增加而失败(在 OP 的情况下)。你能明确地测试一下吗?并且个人而言,虽然这个mindate="#{of:addMonths(now, -12)}" 有效,但我会按照您的建议将它放在模型/支持bean 中。因为它不是 UI/View 的事情(我认为)
  • 我刚刚测试了代码,没有发现任何明显的问题。我同意使用支持 bean 作为巩固过去要求 > 1 年的业务逻辑性质的一种手段。也就是说,我喜欢使用omnifaces,它非常适合这个快速演示! :)
  • 好的...而且 OmniFaces 非常适合 waaaay,而不仅仅是一个快速演示(但它确实在这里节省了代码 ;-))
猜你喜欢
  • 1970-01-01
  • 2013-05-02
  • 2013-07-28
  • 2021-08-15
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 2011-06-18
  • 1970-01-01
相关资源
最近更新 更多