gpt4 book ai didi

jsf - p :dataTable 中的自定义过滤器和转换器

转载 作者:行者123 更新时间:2023-12-05 01:47:55 26 4
gpt4 key购买 nike

由于 PrimeFaces 尚不支持 <p:dataTable> 的转换器过滤器,我正在尝试为 <p:calendar> 实现我自己的自定义过滤器(当然,这个过滤器的设计还是有点难看。它需要应用适当的 CSS,我不能)。

<p:column id="discountStartDate" sortBy="#{row.discountStartDate}" style="width:140px;">
<f:facet name="header">
Start Date<br/>

<p:calendar id="startDateFilter" converter="#{dateTimeConverter}"
timeZone="Asia/Kolkata" locale="#{localeBean.locale}"
pattern="dd-MMM-yyyy hh:mm:ss a"
readonly="#{facesContext.currentPhaseId.ordinal eq 6}"
label="Start Date"
effect="slide" required="true"
size="12"
showButtonPanel="true" navigator="true">

<p:ajax event="dateSelect" listener="#{discountManagedBean.startDateListener}"
onstart="PF('blockDataTableUIWidget').block()"
oncomplete="PF('blockDataTableUIWidget').unblock()"
update="dataTable"/>
</p:calendar>

</f:facet>

<!--No need to refer to-->

<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{row.discountStartDate}" converter="#{dateTimeConverter}"/>
</f:facet>
<f:facet name="input">
<p:tooltip for="dataTableTxtDiscountStartDate" value="#{messages['tooptip.dataTable.popup.calendar']}"/>
<p:calendar id="dataTableTxtDiscountStartDate" binding="#{edStartDate}" value="#{row.discountStartDate}" converter="#{dateTimeConverter}" timeZone="Asia/Kolkata" locale="#{localeBean.locale}" pattern="dd-MMM-yyyy hh:mm:ss a" readonly="#{facesContext.currentPhaseId.ordinal eq 6}" label="#{messages['discount.startdate']}" effect="explode" required="true" showButtonPanel="true" navigator="true"/>
</f:facet>
</p:cellEditor>
</p:column>

当从日历中选择一个日期时,<p:ajax> 中指定的监听器被调用。

public void startDateListener(SelectEvent event)
{
if(event.getObject() instanceof DateTime)
{
//org.joda.time.DateTime
DateTime startDate=(DateTime) event.getObject();
System.out.println(startDate+" : "+startDate.getZone().getID()+ " : "+startDate.getZone());
}
}

所选日期在此方法中检索,但如何在 load() 中使用此日期?方法? <p:dataTable>使用 org.primefaces.model.LazyDataModel<Discount> .

有没有办法从这个监听器方法中使用这个日期,以便数据表 - <p:dataTable>可以在根据日历提供的日期过滤行后更新 - <p:calendar>


如何在覆盖的 load() 中使用此日期?方法?

@Override
public List<Discount> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, String> filters)
{
//Do something with filters to add the date selected from the calendar of the given filter we are talking about.

return discountService.getList(first, pageSize, multiSortMeta, filters);
}

最佳答案

data table filters 有了重大改进在 PrimeFaces 5.0 final released 2014 年 5 月 5 日。

PrimeFaces DataTable Filtering has been a useful feature to quickly filter data using ajax. However there were two main limitations; it was only based on string comparison and no support for custom filtering implementations. Thanks to a PrimeFaces PRO sponsorship, filtering is greatly enhanced for PF5.

Filter Facets

Filtering elements were limited to an input text and a native dropdown, now if an input component is defined using filter facet, it becomes the filter. This enables customizable UIs, ajax update support for filters, using objects instead of simple strings as filter values and more.

http://blog.primefaces.org/?p=3084

例如,

<p:column id="id" headerText="#{messages['id']}" sortBy="#{row.discountId}" filterBy="#{row.discountId}" filterMatchMode="exact">
<f:facet name="filter">
<p:inputText onkeyup="PF('dataTableUIWidget').filter();" converter="javax.faces.Long" class="ui-column-filter"/>
</f:facet>
<h:outputText value="#{row.discountId}"/>
</p:column>

discountId将转换为 Long由指定的转换器。 dataTableUIWidgetwidgetVar<p:dataTable> .

如果是LazyDataModel<T> , Map 的类型过滤器已更改为 Map<String, Object> (来自 Map<String, String> ) load() 的两个重载版本方法。

对于单列排序,

@Override
public List<Type> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
return super.load(first, pageSize, sortField, sortOrder, filters);
}

以及用于多列排序。

@Override
public List<Type> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, Object> filters) {
return super.load(first, pageSize, multiSortMeta, sortOrder, filters);
}

至于问题中的例子,可以改写成下面这样。

<p:column id="discountStartDate" headerText="#{messages['discount.startdate']}" filterBy="#{row.discountStartDate}" sortBy="#{row.discountStartDate}" width="200" style="text-align: right;">

<f:facet name="filter">
<p:calendar id="filterStartDate" converter="#{dateTimeConverter}"
timeZone="Asia/Kolkata" locale="#{localeBean.locale}"
pattern="dd-MMM-yyyy hh:mm:ss a"
readonly="#{facesContext.currentPhaseId.ordinal eq 6}"
label="#{messages['discount.startdate']}"
effect="slide"
size="12"
onclick="PF('filterStartDateWidget').setDate(null);PF('dataTableUIWidget').filter();"
widgetVar="filterStartDateWidget"
showButtonPanel="true" navigator="true">

<p:ajax event="dateSelect"
onstart="PF('dataTableUIWidget').filter();PF('blockDataTableUIWidget').block();"
oncomplete=" PF('blockDataTableUIWidget').unblock();"
onerror="alert('error');"/>
</p:calendar>
</f:facet>

<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{row.discountStartDate}" converter="#{dateTimeConverter}"/>
</f:facet>
<f:facet name="input">
<p:tooltip for="dataTableTxtDiscountStartDate" value="#{messages['tooptip.dataTable.popup.calendar']}"/>
<p:calendar id="dataTableTxtDiscountStartDate" binding="#{edStartDate}" value="#{row.discountStartDate}" converter="#{dateTimeConverter}" timeZone="Asia/Kolkata" locale="#{localeBean.locale}" pattern="dd-MMM-yyyy hh:mm:ss a" readonly="#{facesContext.currentPhaseId.ordinal eq 6}" label="#{messages['discount.startdate']}" effect="explode" required="true" showButtonPanel="true" navigator="true"/>
</f:facet>
</p:cellEditor>
</p:column>

dateTimeConverterorg.joda.time.DateTime 的一种.因此,通过此过滤器的日期将作为 org.joda.time.DateTime 提供。在从 Object 转换之后从过滤器中取出 Map .

关于jsf - p :dataTable 中的自定义过滤器和转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20180501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com