简体   繁体   English

Primefaces dataTable过滤日期

[英]Primefaces dataTable filtering with date

I came accross a particular problem. 我遇到了一个特定的问题。 I have a datatable in which i want to filter dates in primefaces. 我有一个数据表,我想要在primefaces中过滤日期。 when I use 我用的时候

<p:column id="date" headerText="Manufacturing date" 
    filterBy="#{car.dateOfManufacturing}" 
    filterMatchMode="contains">
    <p:outputLabel value="#{car.dateOfManufacturing}"  >
    </p:outputLabel>
</p:column>

Then the filtering of dates happen fine. 然后过滤日期就好了。 But when i use 但是当我使用时

<p:column id="date" headerText="Manufacturing date" 
    filterBy="#{car.dateOfManufacturing}" 
    filterMatchMode="contains">
    <p:outputLabel value="#{car.dateOfManufacturing}"  >
        <f:convertDateTime locale="de"  />
    </p:outputLabel>
</p:column>

the filtering does not happen properly. 过滤不正确。 In fact my observation is with the locale the date format is something like 事实上,我的观察结果与日期格式类似

20.11.2013 二〇一三年十一月二十零日

but even if I type Wed Nov .. i am able to see filtered results. 但即使我输入Wed Nov ..我也能看到过滤结果。 I also observed that without locale the date is dispayed as 我还观察到,如果没有区域设置,则日期会被显示为

Wed Nov 20 13:43:37 CET 2013 So i guess it is getting filtered according the latter date even though we see a different date pattern on the screen. 11月20日星期三13:43:37 CET 2013所以我想即使我们在屏幕上看到不同的日期模式,它也会根据后一个日期进行过滤。

I think need to convert your date before add to filter ( filterBy="#{car.dateOfManufacturing}" ). 我认为需要在添加到过滤器之前转换您的日期( filterBy="#{car.dateOfManufacturing}" )。 One of the simple solution is convert date to string with simple date format in bean. 其中一个简单的解决方案是将日期转换为bean中简单日期格式的字符串。

Here is my code: 这是我的代码:

My RowData containts: String entry1, String entry2, String dateString, Date date . 我的RowData包含String entry1, String entry2, String dateString, Date date

My bean method for fill data: 我的填充数据bean方法:

public List<RowData> getTestData() {
  DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
(...)
  entries.add(new RowData("a1", "b1", dateFormat.format(new Date()), currentDate()));
(...)
return entries;

and my XHTML: 和我的XHTML:

<p:column id="date" headerText="Simple date"
                    filterBy="#{entry.dateString}"
                    filterMatchMode="contains">
         <p:outputLabel value="#{entry.dateString}"  >
         </p:outputLabel>
</p:column>

Now I add your code to my table: 现在我将你的代码添加到我的表中:

<p:column id="dateLocale" headerText="Locale date"
                          filterBy="#{entry.date}"
                          filterMatchMode="contains">
         <p:outputLabel value="#{entry.date}"  >
            <f:convertDateTime locale="de"  />
         </p:outputLabel>
</p:column>

My table: 我的桌子:

在此输入图像描述

Filtering works fine with these three date in locale date column. 过滤在区域设置日期列中与这三个日期一起正常工作。 Perhaps, if I start test this issue with many date, the result will be same which described in your answer. 也许,如果我开始使用许多日期测试此问题,结果将与您的答案中描述的结果相同。

So, I offer covert date to string or use calendar . 所以,我提供隐藏日期字符串或使用日历

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM