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,082,665
  • 372
  • 3,610
  • 3,555
maximus
  • 11,264
  • 30
  • 93
  • 124
  • 2
    Again, what have you tried to accomplish this? Show code instead of just an image. Also, your technology stack won't help us into give you the right directions. – Luiggi Mendoza Jun 05 '13 at 06:12
  • 1
    @maximus Here is an example in plain JSF 2.0 http://stackoverflow.com/a/16762538/354831 it can be modified to your needs. – Alexandre Lavoie Jun 05 '13 at 06:15

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
  • 12,175
  • 14
  • 56
  • 88
  • 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