具有列切换器的Primefaces数据表与列上的排序功能冲突 [英] Primefaces datatable with column toggler conflicts with sort function on column
问题描述
我有一个带有列切换器的数据表.当我取消选中列并在字段上排序时,表是错误的.弹出未选中字段的标题,所有数据向左移动,从而使1列留空.
I have a data table that has a column toggler. When I uncheck a column and sort on a field the table is wrong. The header of the unchecked field pops back up and all data shift to the left, which leaves 1 column empty.
我的table.xhtml文件:
My table.xhtml file:
<h:form>
<p:dataTable id="registrations" var="registration" tableStyle="table-layout: auto;" widgetVar="registrationsTable"
value="#{overviewBean.listOfRegistrations}"
filteredValue="#{overviewBean.filteredListOfRegistrations}" emptyMessage="No registrations found with given criteria" >
<f:facet name="header">
<p:outputPanel style="text-align:left;">
<h:outputText value="Search all fields: " />
<p:inputText id="globalFilter" onkeyup="PF('registrationsTable').filter()" style="width:150px;" placeholder="Enter keyword"/>
<p:commandButton id="toggler" type="button" value="Columns" icon="ui-icon-calculator" style="float:right;"/>
<p:columnToggler datasource="registrations" trigger="toggler" >
</p:columnToggler>
</p:outputPanel>
</f:facet>
<p:column headerText="Active" visible="false">
<h:outputText value="Y" />
</p:column>
<p:column headerText="Firstname" filterBy="#{registration.firstname}" filterStyle="display:none" sortBy="#{registration.firstname}">
<h:outputText value="#{registration.firstname}" />
</p:column>
<p:column headerText="Lastname" filterBy="#{registration.lastname}" filterStyle="display:none" sortBy="#{registration.lastname}">
<h:outputText value="#{registration.lastname}" />
</p:column>
</p:dataTable>
</h:form>
推荐答案
我在此博客文章中找到了答案: http://blog.primefaces.org/?p=3341
I found my answer in this blog post: http://blog.primefaces.org/?p=3341
解决方案是保留后备bean中所有列的Visibility
状态.
The solution was to keep the Visibility
state of all columns in the backing bean.
切换器必须在您的后备Bean中触发onToggle函数:
The toggler must trigger the onToggle function in your backing bean:
<p:columnToggler datasource="registrations" trigger="toggler" >
<p:ajax event="toggle" listener="#{overviewBean.onToggle}" />
</p:columnToggler>
每列必须由备用Bean中的布尔值列表设置:
Each column must be set by the boolean list in the backing bean:
<p:column headerText="Entry date" sortBy="#{registration.entryDate}" visible="#{overviewBean.list[0]}">
<h:outputText value="#{registration.entryDate}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:outputText>
</p:column>
在后备bean中,您必须具有一个表示每个字段可见性的布尔值列表:
In the backing bean you must have a list of booleans which represents the visibility of each field:
private List<Boolean> list;
public List<Boolean> getList() {
return list;
}
public void setList(List<Boolean> list) {
this.list = list;
}
public void onToggle(ToggleEvent e) {
list.set((Integer) e.getData(), e.getVisibility() == Visibility.VISIBLE);
}
在@PostConstruct
方法中,您必须初始化以下bean列表:
In the @PostConstruct
method you must initialize this list of beans:
list = Arrays.asList(false, true, true);
这篇关于具有列切换器的Primefaces数据表与列上的排序功能冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!