显示或隐藏p的选项:inputText - JSF2 Primefaces

时间:2012-09-25 20:21:34

标签: java jsf primefaces

我想知道如何通过选择selectOneMenu选项来显示和隐藏字段(p:inputText)。例如:我有一个selectOneMenu从1到10,点击10显示10 inputText。单击1仅显示1 inputText。

我已经有了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}" /> 

谢谢!

2 个答案:

答案 0 :(得分: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 :(得分:-1)

尝试使用&#34;渲染&#34;和&#34;更新&#34;对于那个标签。