开发者

jsf: generate another panelGrid

My code:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<f:view>
 <html>
  <body>
   <h:form>
<h:panelGrid columns="3" border="1" rules="all" title="This is panelGroup demo">
  <f:facet name="header">开发者_如何学编程
  <h:outputText value="Submit Detail"/>
  </f:facet>
   <h:inputText/>
   <h:inputText/>
   <h:inputText/>
   <h:inputText/>
   <h:inputText/>
   <h:inputText/>
   <h:inputText/>
   <h:inputText/>
  <f:facet name="footer">
  <h:panelGroup>
   <h:outputText value="Leave Comment Here :" />
   <h:inputText/>
   <h:outputText value="Submit" />
   <h:commandButton value="SUBMIT" />
  </h:panelGroup>
  </f:facet>
</h:panelGrid> 
  </h:form>
  </body>
 </html>
</f:view>

I want to put a commandButton tag. When I click on the button it should generate another panelgrid with the same number of columns and rows (Each time I click). what should I add to realize it?


You can put you panelGrid inside a dataTable and then wire it to a bean that has a list in it. Then, when you press the commandButton all you need to do is add an element to the list and the data will render an additional row. Here is some example code:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<f:view>
 <html>
  <body>
   <h:form>
    <h:dataTable value="#{myBean.myList}" var="myListElement" >
     <h:column>
      <!-- put your panelGrid here!! -->
     </h:column>
    </h:dataTable>
    <h:commandButton value="Add panelGrid" action="#{myBean.addToMyList}" />
   </h:form>
  </body>
 </html>
</f:view>

Each element in the list can then be used to hold information that is to be displayed in the individual panelGrids.

EDIT: Backing bean code.

public class MyBean {
    private List<Object> myList;

    public MyBean() {
        myList = new ArrayList<Object>();
    }

    public void addToMyList() {
        myList.add(new Object());
    }

    public List<Object> getMyList() {
        return (myList);
    }
}

To understand this code I suggest you read about dataTable here and here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜