我有一个页面,我点击一个按钮,一个panelgrid动态添加到视图。我使用了一个UI:Reapeat标签,连接到viewScope中的一个支持bean来实现它。这有效,没有任何缺陷。当我想删除其中一个添加的网格时,会出现问题。
我已经阅读了很多关于类似问题的Balus C的回答,包括使用JSTL c:foreach,但没有运气来修复我自己的问题。你们其中一个人可能很容易,但对我来说,这似乎是不可能的。这是我的XHTML:
<h:form id="mainForm">
<div id="xmlHead" class="xmlTopLabel">
<p:outputLabel>Object Name</p:outputLabel>
</div>
<div class="xmlTopLabel">
<p:outputLabel>Attribute Name</p:outputLabel>
</div>
<div class="clear">
</div>
<div id="xmlHead" class="xmlTop">
<p:inputText></p:inputText>
</div>
<div class="xmlTop">
<p:inputText></p:inputText>
</div>
<div class="clear">
</div>
<h:panelGroup id="tagsList" class="xmlTagsBox">
<p:commandButton action="#{controller.add()}" value="add" update="tagsList" process="@this,tagsList"></p:commandButton>
<p:commandButton action="#{controller.execute()}" value="execute"></p:commandButton>
<ui:repeat var="tag" value="#{controller.xmlTagsList}" varStatus="currIndex" >
<h:panelGrid id="grid" columns="11" style="margin-bottom:5px" cellpadding="5">
<h:outputText value="Tag Name " />
<p:inputText value="#{tag.tagName}"></p:inputText>
<h:outputText value="Value " />
<p:inputText value="#{tag.value}"></p:inputText>
<h:outputText value="Type " />
<p:selectOneMenu value="#{tag.dataType}">
<f:selectItem itemLabel="String" itemValue="String" />
<f:selectItem itemLabel="Integer" itemValue="Integer" />
<f:selectItem itemLabel="Double" itemValue="Double" />
<f:selectItem itemLabel="Boolean" itemValue="Boolean" />
</p:selectOneMenu>
<h:outputText value="is list " />
<p:selectBooleanCheckbox id="isList" value="#{tag.list}"></p:selectBooleanCheckbox>
<h:outputText value="is ChildNode " />
<p:selectBooleanCheckbox id="isChildNode" value="#{tag.child}"></p:selectBooleanCheckbox>
<p:commandButton action="#{controller.remove(currIndex.index)}" value="remove" update ="tagsList">
</p:commandButton>
</h:panelGrid>
</ui:repeat>
</h:panelGroup>
这是我的支持豆:
@ManagedBean
@ViewScoped
public class Controller implements Serializable{
/** The instance represents the */
private static final long serialVersionUID = -6907079630004212317L;
private List<Tags> xmlTagsList;
private XmlObject xmlObj;
public void init(){
if(xmlTagsList == null){
xmlObj = new XmlObject();
xmlTagsList = new ArrayList<Tags>();
for(int i = 0; i < 4; i++){
xmlTagsList.add(xmlObj.new Tags());
}
}else{
}
}
public void add(){
xmlTagsList.add(xmlObj.new Tags());
System.out.println("new obj added "+xmlTagsList.size());
}
public void remove(int index){
xmlTagsList.remove(index);
System.out.println(index);
System.out.println(xmlTagsList.size());
}
public void execute(){
for(Tags tag : xmlTagsList){
System.out.println(tag.getTagName());
}
}
/**
* @return the xmlTagsList
*/
public List<Tags> getXmlTagsList(){
return xmlTagsList;
}
/**
* @return the xmlObj
*/
public XmlObject getXmlObj(){
return xmlObj;
}
/**
* @param xmlTagsList the xmlTagsList to set
*/
public void setXmlTagsList(List<Tags> xmlTagsList){
this.xmlTagsList = xmlTagsList;
}
/**
* @param xmlObj the xmlObj to set
*/
public void setXmlObj(XmlObject xmlObj){
this.xmlObj = xmlObj;
}
}
任何帮助都会受到很多关注。我知道删除方法有效,因为如果在点击删除按钮后点击添加按钮,则会更新panelGroup并删除我删除的网格。这就是为什么我知道它与视图有关,而且我可能正在做一些愚蠢的事情。
我正在使用mojorra 2.2 PrimeFaces 4.0,tomcat 8。