我有一个要求,比如我需要在点击按钮时显示/隐藏数据表。我尝试实现它,但它无法正常工作。下面是代码。
如果我们能用ajax做,请告诉我。只有当我将ajax设置为false时才有可能。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>hello world</title>
</h:head>
<h:body>
<h:form>
<p:outputPanel id="panel" rendered="#{bye1.showtable}">
<p:dataTable value="#{bye1.carmodel}" var="cartypes">
<p:column headerText="Model">
<h:outputText value="#{cartypes.carname}">
</h:outputText>
</p:column>
<p:column headerText="Location">
<h:outputText value="#{cartypes.location}"></h:outputText>
</p:column>
<p:column headerText="Price">
<h:outputText value="#{cartypes.rate}"></h:outputText>
</p:column>
</p:dataTable>
</p:outputPanel>
<p:commandButton value="show" action="#{bye1.enabletable}" update="panel">
</p:commandButton>
</h:form>
</h:body>
</html>
package lifecycle;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
public class bye {
private String output;
private Boolean showtable;
private List<cars> carmodel;
public List<cars> getCarmodel() {
System.out.println("cars populated...........");
return carmodel;
}
@PostConstruct
public void bye1() {
System.out.println("constructor called");
carmodel = new ArrayList<cars>();
output = "hai";
carmodel.add(new cars("ford","chennai","4 laks"));
carmodel.add(new cars("AUDI","chennai","44 laks"));
}
public String getOutput() {
return output;
}
public Boolean getShowtable() {
return showtable;
}
public String enabletable() {
showtable = true;
return "";
}
}
任何帮助?
先谢谢
答案 0 :(得分:0)
在命令按钮上使用actionListener而不是action。
“空字符串的返回值或相同的视图ID也将返回到同一页面,但会重新创建视图范围,从而销毁任何当前活动的视图范围bean,并在适用的情况下重新创建它们:” Differences between action and actionListener
还要考虑使用Boolean vs boolean。
您的类似乎缺少@ManagedBean和范围注释?
答案 1 :(得分:0)
我认为update="panel
&#34;工作不正常,panel
组件未更新。当你禁用ajax时,整个页面都会得到更新,也许这就是为什么更新只适用于ajax =&#34; false&#34;。
你可以尝试一下,告诉我它现在是否有效吗? :
<p:commandButton value="show" actionListener="#{bye1.enabletable}" ajax="true" update=":#{p:component('panel')}">
</p:commandButton>
答案 2 :(得分:0)
我找到了这个解决方案。
我做了这样的改变
<p:outputPanel id="panel" >
<p:dataTable value="#{bye1.carmodel}" var="cartypes"
rendered="#{bye1.showtable}">
..............
</p:dataatble>
</p:outputPanel>
将呈现的属性设置为数据表而不是输出面板。