2

I want to add dynamically input fields. Like

enter image description here

Is there a good component in PF on how to add such a component?

Pls give me a hint on how you would develop it, cause I have no clue at the moment.

I really appreciate your answer.

My technology stack:

  • Hibernate: 4.0.1.Final
  • Spring: 3.1.1.RELEASE
  • Primefaces: 3.5
  • jsf-version: 2.2.0-m08
  • PrimefacesMobile-version: 0.9.3
  • Apache Tomcat/7.0.12
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
maximus
  • 10,894
  • 28
  • 91
  • 124

1 Answers1

8

Maybe the following piece of code can help you out, I'm afraid there's not a component for this (at least to my knowledge):

HTML

<h:form>
    <ui:repeat value=#{bean.values} 
               var="value">
        <h:inputText value="#{value}" />
        <br />
    </ui:repeat>

    <h:commandButton value="Extend">
        <f:ajax listener="#{bean.extend}"
                process="@form" 
                render="@form" />
    </h:commandButton>
    <h:commandButton action="#{bean.submit}" 
                     value="Save" />
</h:form>

BEAN

@ManagedBean
@ViewScoped
public class Bean {
    private List<String> values;

    @PostConstruct
    public void init() {
        values = new ArrayList();
        values.add("");
    }

    public void submit() {
        // save values in database
    }

    public void extend() {
        values.add("");
    }

    public void setValues(List<String> values) {
        this.values = values;
    }

    public List<String> getValues() {
        return values;
    }
}
Menno
  • 11,415
  • 13
  • 54
  • 86
  • 1
    Please refer to [JSTL in JSF2 Facelets… makes sense?](http://stackoverflow.com/q/3342984/1065197) to understand that using `` in this case would be a problem. Instead, replace it with `` to avoid ajax render/update problems. Otherwise, please test your answer before posting it. – Luiggi Mendoza Jun 05 '13 at 06:17
  • @LuiggiMendoza Good call, started from a situation in which `` was necessary due to the phase. In this case `` will work just fine! About the testing, have no IDE nearby. – Menno Jun 05 '13 at 06:23
  • I tried this but it doesn't work when you have a List. You can't change the strings on the list since they are immutable, so new values won't get to the backing bean. – JSeven Apr 29 '14 at 21:09