I have a page where I click on a button and a panelgrid is added dynamically to the view. I use a UI:Reapeat tag, connected to a backing bean in viewScope to achive it. This works, without any flaws. The problem appears when I want to remove one of the added grids.
I have read alot of Balus C's answeres about similar issues, including using JSTL c:foreach, but with no luck of fixing my own. It is probably easy for one of you guys but for me, it seems impossible. Here is my 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>
And here is my backing bean:
@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;
}
}
Any help would be much appriciated. I know that the remove method works, because if i hit the add button after I hit the remove button, the panelGroup is updated and the grids I removed is being removed. That's why I know it has to do with the view, and that I am probably doing something stupid.
I am using mojorra 2.2 PrimeFaces 4.0, tomcat 8.