2012-09-25 78 views
0

我想知道如何通過選擇選項selectOneMenu來顯示和隱藏字段(p:inputText)。例如:我有一個從1到10的selectOneMenu,點擊10顯示10個inputText。點擊1只出現1個inputText。顯示或隱藏選項p:inputText - JSF2 Primefaces

我已經有SectOneMenu和inputText的(如下圖):

  <p:selectOneMenu value="#{bean.parametro.intervalo}"> 
       <f:selectItem itemLabel="Select One" itemValue="" /> 
       <f:selectItem itemLabel="1" itemValue="1" /> 
       <f:selectItem itemLabel="2" itemValue="2" /> 
       <f:selectItem itemLabel="3" itemValue="3" /> 
       <f:selectItem itemLabel="4" itemValue="4" /> 
       <f:selectItem itemLabel="5" itemValue="5" /> 
       <f:selectItem itemLabel="6" itemValue="6" /> 
       <f:selectItem itemLabel="7" itemValue="7" /> 
       <f:selectItem itemLabel="8" itemValue="8" /> 
       <f:selectItem itemLabel="9" itemValue="9" /> 
       <f:selectItem itemLabel="10" itemValue="10" /> 
      </p:selectOneMenu> 

的inputText被複制:

   <h:outputText value="portabilidadeGrupo" /> 
       <p:selectBooleanCheckbox value="#{bean.parametro.portabilidadeGrupo}" /> 

       <h:output Label for="numInicial" value="Nº Inicial Int:" /> 
       <p:inputText id="numInicial" value="#{bean.parametro.numInicial}" /> 

       <h:outputLabel for="numFinal" value="Nº Final Int:" /> 
       <p:inputText id="numFinal" value="#{bean.parametro.numFinal}" /> 

       <h:outputLabel for="idGrupo" value="Id do Grupo:" /> 
       <p:inputText id="idGrupo" value="#{bean.parametro.idgrupo}" /> 

       <h:outputText value="PTO" /> 
       <p:selectBooleanCheckbox value="#{bean.parametro.pto}" /> 

謝謝!

+0

就像我在「selectOneMenu」中點擊「3」,那麼會顯示3個inputText元素?例如''''''或者如果我點擊「3」,那麼會顯示「inputText」 ? '' – Fallup

回答

0

我放棄了使用selectOneMenu並使用這個Darryl Nortje動態字段的例子解決了我的問題。

package datatable; 

import java.util.ArrayList; 
import java.util.List; 

import javax.faces.component.html.HtmlDataTable; 

public class DatatableBean { 

    //Data table contents 
    private List<Person> people; 
    //Data table binding. TO figure out which row was acted on. 
    private HtmlDataTable table; 

    //vars to add a person. 
    private String firstname; 
    private String surname; 

    public DatatableBean() { 
    people = new ArrayList<Person>(); 
    } 

    //ACTION METHODS TO ADD AND REMOVE A PERSON 
    public void removePerson() { 
    //FIRST figure out which row was acted on. Then remove that person from the people list. 
    Person selectedPerson = (Person) table.getRowData(); 
    people.remove(selectedPerson); 
    //simple hey. There is another way to do this with binding the datatable to the bean. 
    } 

    public void addPerson() { 
    //here we create a new person object from the entered values, and add to the people list. 
    Person newPerson = new Person(); 
    newPerson.setFirstname(getFirstname()); 
    newPerson.setSurname(getSurname()); 
    people.add(newPerson); 
    } 

    //HELPERS FOR DISPLAYING NICELY 
    public boolean isPersonAdded() { 
    return people.size() > 0; 
    } 

    //ALL GETTERS AND SETTERS HERE.... 

} 

JSP

<%@ page isELIgnored="false" language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> 
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t" %> 


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<f:view> 
<head> 
    <title>Data table stuff</title> 
</head> 
<body> 

<div class="text_style"> 

    <h:form id="mainform"> 


<h:dataTable value="#{datatableBean.people}" var="tab" 
binding="#{datatableBean.table}" rendered="#{datatableBean.personAdded}"> 

<h:column> 
<f:facet name="header"> 
      <h:outputText value="Name"/> 
     </f:facet> 
     <h:outputText value="#{tab.firstname}"/> 
</h:column> 

<h:column> 
<f:facet name="header"> 
      <h:outputText value="Surname"/> 
     </f:facet> 
     <h:outputText value="#{tab.surname}"/> 
</h:column> 

<h:column> 
<f:facet name="header"> 
<h:outputText value="Click to remove"/> 
</f:facet> 

<h:commandLink action="#{datatableBean.removePerson}" value="remove"/> 
</h:column> 

</h:dataTable> 

<p> 
<h:outputText value="To add a new person, fill in these details then click add"/> 

<p> 
<h:outputText value="Firstname"/> 
<h:inputText value="#{datatableBean.firstname}" /> 
<br> 
<h:outputText value="Surname"/> 
<h:inputText value="#{datatableBean.surname}"/> 
<br> 
<h:commandButton action="#{datatableBean.addPerson}" value="Add"/> 


</h:form>  
</div> 
</body> 
</f:view> 
</html> 
-1

嘗試使用 「渲染」 和 「更新」 爲標籤。