简体   繁体   English

jsf primefaces数据表过滤问题

[英]jsf primefaces datatable filtering issues

I am using primefaces and its datatable. 我正在使用primefaces及其数据表。 A few columns are dates or currencies. 几列是日期或货币。 If I try to filter those, there are awkward behaviours. 如果我试图过滤那些,那就有尴尬的行为。 When I start typing the filter works until the first delimiter (dot for date for example, so it only filters for 11. the next character let the table to display no entry). 当我开始输入时,过滤器一直工作到第一个分隔符(例如日期点,所以它只过滤11个。下一个字符让表格显示没有条目)。

Is it possible to apply a dateconverter? 是否可以应用日期转换器?

Here is my code for now: 这是我现在的代码:

<p:column filterBy="#{cou.startDate}"
    headerText="#{text['date']}"
    filterMatchMode="contains" 
    sortBy="#{cou.startDate}" >
        <h:outputText value="#{cou.startDate}" >
             <f:convertDateTime pattern="dd.MM.yyyy" />
        </h:outputText>
</p:column> 

Instead of directly using the cou.startDate from the model, you can instead do the following: 您可以改为执行以下操作,而不是直接使用模型中的cou.startDate:

Create a new transient property in the model class. 在模型类中创建新的瞬态属性。

@Transient
private String dateForFilter;
public String getDateForFilter() {
 return dateForFilter;
}
public void setDateForFilter(String dateForFilter) {
 this.dateForFilter = dateForFilter;
}

Create the logic below before returning the data model. 在返回数据模型之前创建下面的逻辑。

public List<Item> getDataModel() {
   List<Item> lstItem = serviceClass.loadItem(userid);
   for (Item item : lstItem) {
      DateFormat dateFormat = null;
      Date date = item.getDate;
      dateFormat = new SimpleDateFormat("MM/dd/yyyy kk:mm");
      item.setDateForFilter(dateFormat.format(date));
   }

   return lstItem;
}

Update your XHTML to use the dateForFilter property. 更新您的XHTML以使用dateForFilter属性。

<p:column filterBy="#{item.dateForFilter}">
  <f:facet name="header">
    Transaction Date
  </f:facet>
  <h:outputText value="#{item.dateForFilter}" />
</p:column>

Note: You can only use this if you're not using the date to update the content of the model class. 注意:如果您没有使用日期来更新模型类的内容,则只能使用此选项。

HTH. HTH。

There is no ready-made date filter mechanism in primefaces yet, but there is a possibility to filter by date using a custom filter. 在primefaces中还没有现成的日期过滤机制,但是有可能使用自定义过滤器按日期过滤。 You will have to define a header facet for your column and use ajax calls for "manual" filtering, but it does work: 您必须为列定义标题构面并使用ajax调用进行“手动”过滤,但它确实有效:

<f:facet name="header">DateRange
  <div>
    <p:calendar id="from" value="#{bean.from}" styleClass="calendarFilter">
      <p:ajax event="dateSelect" listener="#{ctrlr.filterDates()}" update="dataTableId"/>
    </p:calendar>
    <p:calendar id="to" value="#{bean.to}" styleClass="calendarFilter">
      <p:ajax event="dateSelect" listener="#{ctrlr.filterDates()}" update="dataTableId"/>
    </p:calendar>
  </div>
</f:facet>

As far as I know, you can't use a converter for the filter value. 据我所知,您不能使用转换器作为过滤器值。 You can, however, deal with that in your bean/service/dao logic. 但是,您可以在bean / service / dao逻辑中处理它。

You could hardcode your logic and use SimpleDateFormat to parse the value if the filter column matches certain name like startDate or endDate. 如果过滤器列匹配某些名称(如startDate或endDate),则可以对逻辑进行硬编码并使用SimpleDateFormat来解析值。 A more generic approach would be to use reflection to get the Class associated with the column and use SimpleDateFormat if it's Date, DecimalFormat if it's a number and so on. 更通用的方法是使用反射来获取与列关联的类,如果是Date,则使用SimpleDateFormat,如果是数字则使用DecimalFormat,依此类推。

Naturally if you're propagating that query to a database, you won't be able to use the like operator. 当然,如果您将该查询传播到数据库,您将无法使用like运算符。 If you're using a number you'll need to compare for equality (same applies to dates). 如果您使用的是数字,则需要比较相等(同样适用于日期)。 If you're looking for stuff that's in memory you'll have to change you logic a little bit. 如果你正在寻找内存中的东西,你将不得不改变你的逻辑。 But it shouldn't be too bad. 但它不应该太糟糕。 If you could post some of your backing bean/service code, I guess I could be a little more helpful ;) 如果你可以发布一些你的支持bean /服务代码,我想我可能会更有帮助;)

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

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