6

データテーブルに列があり、次のコードでフィルターを追加したい:

<p:dataTable id="aDataTable" var="aVariable" value="#{aView.values}" paginator="true" rows="10" selectionMode="single" selection="#{aView.selection}" onRowSelectUpdate="aForm">
                 <f:facet name="header">
                     A List
                 </f:facet>
              <p:column sortBy="#{aVariable.id}" filterBy="#{aVariable.id}" filterEvent="change">
                  <f:facet name="header">
                      <h:outputText value="No"/>
                  </f:facet>
                   <h:outputText value="#{aVariable.id}"/>
              </p:column>
              <p:column sortBy="#{aVariable.date}" filterBy="#{aVariable.date}" filterEvent="change">
                  <f:facet name="header">
                      <h:outputText value="Date"/>
                  </f:facet>
</p:dataTable>

日付は、次の形式でフォームに入力されます。

<h:outputText value="Date: *"/>
<p:calendar pattern="dd/MM/yyyy" value="#{aView.value.date}"/>

フィルターは ID に対しては機能しますが、日付に対しては機能しません。この理由は何ですか?この場合、フィルターを機能させるにはどうすればよいですか?

4

2 に答える 2

11

Primefaces には既製の日付フィルター メカニズムはまだありませんが、カスタム フィルターを使用して日付でフィルター処理する可能性があります。列のヘッダー ファセットを定義し、「手動」フィルタリングに ajax 呼び出しを使用する必要がありますが、機能します。

<column>
  <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>

...

于 2012-06-01T07:26:51.347 に答える
7

より簡単な解決策として、filterBy コンポーネントを使用するためのデータ型として String を持つ日付のプレースホルダーを追加するだけです。

手順:

モデル クラスに新しい一時プロパティを作成します。

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

データ モデルを返す前に、以下のロジックを作成します。

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;
}

dateForFilter プロパティを使用するように XHTML を更新します。

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

注: これは、日付を使用してモデル クラスのコンテンツを更新しない場合にのみ使用できます。

HTH。

于 2012-08-03T07:28:31.070 に答える