Java ';显示所有';Primefaces数据表的按钮
我想为primefaces数据表设置一个“全部显示”按钮,但是,我遇到了一些问题。下面是一些演示问题的测试代码: Test.xhtml:Java ';显示所有';Primefaces数据表的按钮,java,primefaces,Java,Primefaces,我想为primefaces数据表设置一个“全部显示”按钮,但是,我遇到了一些问题。下面是一些演示问题的测试代码: Test.xhtml: ... <h:form> <p:selectBooleanCheckbox value="#{testBean.showAll}" itemLabel="Show All"> <p:ajax update="@f
...
<h:form>
<p:selectBooleanCheckbox value="#{testBean.showAll}" itemLabel="Show All">
<p:ajax update="@form"/>
</p:selectBooleanCheckbox>
<p:panel id="test">
<p:dataTable id="values" var="value" value="#{testBean.fullList}" filteredValue="#{testBean.filteredList}" paginatorAlwaysVisible="false"
paginator="#{!testBean.showAll}" rows="#{!testBean.showAll ? 2 : null }" widgetVar="valuesTable"
emptyMessage="No records found.">
<p:column id="enum" sortBy="#{value.toString()}" filterBy="#{value.toString()}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Enum" />
</f:facet>
<h:outputText value ="#{value.toString()}"/>
</p:column>
<p:column id="name" sortBy="#{value.name}" filterBy="#{value.name}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{value.name}" />
</p:column>
</p:dataTable>
</p:panel>
</h:form>
...
。。。
...
下面是TestBean.java:
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
@Named(value = "testBean")
@SessionScoped
public class TestBean implements Serializable {
public static enum Value {
ONE, TWO, THREE;
public String getName() {
return name().toLowerCase();
}
@Override
public String toString() {
return this.name();
}
}
/**
* Creates a new instance of TestBean
*/
public TestBean() {
}
private boolean showAll = false;
private List<Value> fullList = Arrays.asList(Value.values());
private List<Value> filteredList;
public boolean isShowAll() {
return showAll;
}
public void setShowAll(boolean showAll) {
this.showAll = showAll;
}
public List<Value> getFullList() {
return fullList;
}
public void setFullList(List<Value> fullList) {
this.fullList = fullList;
}
public List<Value> getFilteredList() {
return filteredList;
}
public void setFilteredList(List<Value> filteredList) {
this.filteredList = filteredList;
}
}
import javax.inject.Named;
导入javax.enterprise.context.SessionScoped;
导入java.io.Serializable;
导入java.util.array;
导入java.util.List;
@命名(value=“testBean”)
@会议范围
公共类TestBean实现了可序列化{
公共静态枚举值{
一,二,三,;
公共字符串getName(){
返回name().toLowerCase();
}
@凌驾
公共字符串toString(){
返回此.name();
}
}
/**
*创建TestBean的新实例
*/
公共TestBean(){
}
私有布尔showAll=false;
私有列表fullList=Arrays.asList(Value.values());
私有列表过滤器列表;
公共布尔值isShowAll(){
返回showAll;
}
公共void setShowAll(布尔showAll){
this.showAll=showAll;
}
公共列表getFullList(){
返回完整列表;
}
公共void setFullList(列表fullList){
this.fullList=完整列表;
}
公共列表getFilteredList(){
返回过滤器列表;
}
public void setFilteredList(列表filteredList){
this.filteredList=filteredList;
}
}
如果我不更改选项卡,页面将按预期工作:切换“全部显示”按钮将更新表格以显示所有3个值,或仅显示两个值。但是,如果未选中show all(仅显示2行),并且我单击表的第2页以查看第三条记录,然后单击“show all”,则表不会正确更新。它从表的顶部删除分页器(如预期的那样),但仍然只显示第三条记录。如果我取消选中ShowAll并导航回datatable的第一页,它现在也被破坏了
我尝试将ajax update语句更改为表的id,但这并没有改变结果
我做错什么了吗?提前感谢。您必须以编程方式直接在
DataTable
组件上设置所需的值(我首先使用了显式和行
bean属性以提高可读性):
及
@Named
@视域
公共类TestBean实现了可序列化
{
私有静态最终长serialVersionUID=1L;
公共静态枚举值
{
一,二,三,;
公共字符串getName()
{
返回name().toLowerCase();
}
@凌驾
公共字符串toString()
{
返回名称();
}
}
私有布尔showAll=false;
私有int first=0;
私有int行=2;
私有列表fullList=Arrays.asList(Value.values());
私有列表过滤器列表;
公共布尔值isShowAll()
{
返回showAll;
}
公共void setShowAll(布尔showAll)
{
this.showAll=showAll;
第一个=0;
行=showAll?0:2;
filteredList=null;
//获取FacesContext实例
FacesContext context=FacesContext.getCurrentInstance();
//获取当前组件(p:selectBooleanCheckbox)
UIComponent=UIComponent.getCurrentComponent(上下文);
//在同一NamingContainer中查找DataTable
DataTable=(DataTable)组件。findComponent(“值”);
//重置第一行索引
表1.setFirst(第一);
//重置最后一行索引
表1.setRows(行);
//重置过滤器值
表.setFilteredValue(null);
}
//所有其他的接受者/接受者
}
使用JSF 2.3.0-m06(Mojarra)和PrimeFaces 6.0.2在Wildfly 10.0.0.Final上测试,我遇到了相同的问题,通过以下步骤解决:
将styleClass=“all paginator”添加到数据表
<p:dataTable id="values" var="value" value="#{testBean.fullList}"
filteredValue="#{testBean.filteredList}" styleClass="all-paginator"..>
在datatable属性“rowsPerPageTemplate”的末尾添加recordNumber的属性
您可以尝试使用不同范围的@SessionScoped将其更改为@ViewScoped@YouceFladani没有工作:/谢谢你的建议!使用此代码,点击“全部显示”将如何更改fullList
字段?它不应更改fullList
字段。单击“全部显示”应删除分页。这是一种解决方法,但您可以尝试在p:selectBooleanCheckbox中添加onchange=“PF('valuesTable').paginator().setPage(0)”,以便在刷新之前将dataTable页面设置为0。这很好,谢谢!虽然我确实希望有一个更简单的方法。
@Named
@ViewScoped
public class TestBean implements Serializable
{
private static final long serialVersionUID = 1L;
public static enum Value
{
ONE, TWO, THREE;
public String getName()
{
return name().toLowerCase();
}
@Override
public String toString()
{
return name();
}
}
private boolean showAll = false;
private int first = 0;
private int rows = 2;
private List<Value> fullList = Arrays.asList(Value.values());
private List<Value> filteredList;
public boolean isShowAll()
{
return showAll;
}
public void setShowAll(boolean showAll)
{
this.showAll = showAll;
first = 0;
rows = showAll ? 0 : 2;
filteredList = null;
// get the FacesContext instance
FacesContext context = FacesContext.getCurrentInstance();
// get the current component (p:selectBooleanCheckbox)
UIComponent component = UIComponent.getCurrentComponent(context);
// find DataTable within the same NamingContainer
DataTable table = (DataTable) component.findComponent("values");
// reset first row index
table.setFirst(first);
// reset last row index
table.setRows(rows);
// reset filterd value
table.setFilteredValue(null);
}
// all other getters/setters
}
<p:dataTable id="values" var="value" value="#{testBean.fullList}"
filteredValue="#{testBean.filteredList}" styleClass="all-paginator"..>
public int getRecordNumber() {
if(CollectionUtils.isEmpty(fullList)){
return 0;
}
return fullList.size();
}
<p:dataTable id="values" var="value" value="#{testBean.fullList}" filteredValue="#{testBean.filteredList}"
styleClass="all-paginator" rowsPerPageTemplate="20,30,40,50,#{testBean.recordNumber}"..>