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
2
  • 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. Jun 5, 2013 at 6:12
  • 1
    @maximus Here is an example in plain JSF 2.0 stackoverflow.com/a/16762538/354831 it can be modified to your needs. Jun 5, 2013 at 6:15

1 Answer 1

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;
    }
}
3
  • 1
    Please refer to JSTL in JSF2 Facelets… makes sense? to understand that using <c:forEach> in this case would be a problem. Instead, replace it with <ui:repeat> to avoid ajax render/update problems. Otherwise, please test your answer before posting it. Jun 5, 2013 at 6:17
  • @LuiggiMendoza Good call, started from a situation in which <c:forEach> was necessary due to the phase. In this case <ui:repeat> will work just fine! About the testing, have no IDE nearby.
    – Menno
    Jun 5, 2013 at 6:23
  • I tried this but it doesn't work when you have a List<String>. 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, 2014 at 21:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.