Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can better approach this with a backing bean than using Javascript. Using ajax you wouldn't need any page refreshes. Something along the lines of this:</p> <p><strong>HTML</strong></p> <pre><code>&lt;h:form&gt; &lt;p&gt; &lt;h:inputText value="#{bean.noOfFields}" /&gt; &lt;h:commandButton value="Create fields"&gt; &lt;f:ajax execute="@form" render="@form" /&gt; &lt;/h:commandButton&gt; &lt;/p&gt; &lt;hr /&gt; &lt;p&gt; &lt;c:forEach items=#{bean.values} varStatus="counter"&gt; Field no. #{counter.index} &lt;h:inputText value="#{bean.values[counter.index}" /&gt;&lt;br /&gt; &lt;/c:forEach&gt; &lt;h:commandButton action="#{bean.submit}" value="Save" /&gt; &lt;/p&gt; &lt;/h:form&gt; </code></pre> <p><strong>Bean.java</strong></p> <pre><code>@ManagedBean @ViewScoped public class Bean { private String noOfFields = 1; private String[] values = new String[1]; public void submit() { // save values in database } public String getNoOfFields() { return noOfFields; } public void setNoOfFields(String noOfFields) { try { values = new String[Integer.valueOf(noOfFields)]; this.noOfFields = noOfFields; catch(NumberFormatException ex) { values = new String[1]; noOfFields = "1"; } } public String[] getValues() { return values; } } </code></pre> <p><strong>Note</strong></p> <p>In case you want to stick to a keyup event, you can easily bind this to <code>&lt;h:inputText value="#{bean.noOfFields}" /&gt;</code> too. Though I'd recommend not doing this, since every keystroke will invoke another ajax call.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload