Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF adding textfields dynamically on click of a button
    primarykey
    data
    text
    <p>I have written the following code so that I can have a single textfield followed by a add button and a save button at the bottom.</p> <p>I want the first textfield and add button to be fixed, but whenever a user cicks on add button, a text field gets added below the present textfield and the add button and save button goes down.</p> <p>I have the following piece of code, but it doesnt seem to working.</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:c="http://java.sun.com/jstl/core" xmlns:ui="http://java.sun.com/jsf/facelets"&gt; &lt;h:head&gt; &lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Dashboard | BlueWhale Admin&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" href="../css/reset.css" media="screen" /&gt; &lt;link rel="stylesheet" type="text/css" href="../css/text.css" media="screen" /&gt; &lt;link rel="stylesheet" type="text/css" href="../css/grid.css" media="screen" /&gt; &lt;link rel="stylesheet" type="text/css" href="../css/layout.css" media="screen" /&gt; &lt;link rel="stylesheet" type="text/css" href="../css/nav.css" media="screen" /&gt; &lt;/h:head&gt; &lt;body&gt; &lt;h:form&gt; &lt;hr/&gt; &lt;h:dataTable id="newsinputs" value="#{newsAlerts.values}" var="item" cellspacing="10"&gt; &lt;h:column&gt; &lt;h:outputLabel value="#{item.label}" /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/h:column&gt; &lt;h:column&gt; &lt;h:inputText value="#{item.news}" size="100" /&gt;&lt;br/&gt; &lt;/h:column&gt; &lt;/h:dataTable&gt; &lt;h:commandButton styleClass="btn btn-blue" action="#{newsAlerts.add()}" value="Add"&gt;&lt;/h:commandButton&gt; &lt;hr/&gt; &lt;h:commandButton styleClass="btn btn-blue" action="#{newsAlerts.submit}" value="Save" /&gt; &lt;/h:form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The bean class is as follows</p> <pre><code>package com.kc.aop.bean; import java.util.ArrayList; import java.util.List; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import com.kc.aop.VO.NewsVO; @ManagedBean(name = "newsAlerts") @ViewScoped public class News { private List&lt;NewsVO&gt; values; public News() { this.values = new ArrayList&lt;NewsVO&gt;(); NewsVO newsVO = new NewsVO(); newsVO.setLabel("News "+ this.values.size()+1); getValues().add(newsVO); } public String submit() { for(NewsVO newsVO : this.values) { System.out.println(newsVO.getNews()); System.out.println(newsVO.getLabel()); } return null; // save values in database } public List&lt;NewsVO&gt; getValues() { return values; } public void setValues(List&lt;NewsVO&gt; values) { this.values = values; } public String add() { NewsVO newsVO = new NewsVO(); newsVO.setLabel("News "+ this.values.size()+1); this.values.add(newsVO); return "success"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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