Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd a new row to a p:datatable then submit content
    primarykey
    data
    text
    <p>I'm trying to add a new row in a Primefaces datatable, then I would like to submit the content of this table and do some business logic. The datatable model is a collection that is maintained in a ViewScoped managed bean.</p> <p>I'm using JSF 2.1 with Primefaces 3.3.</p> <p>Short example:</p> <pre><code> &lt;h:form id="my-form"&gt; &lt;p:dataTable value="#{testBean.list}" var="s" id="datatable"&gt; &lt;p:column&gt; &lt;h:inputText value="#{s}"/&gt; &lt;/p:column&gt; &lt;f:facet name="footer"&gt; &lt;p:commandButton value="Add row" action="#{testBean.addRow()}" process="@form" update="@form" immediate="true" /&gt; &lt;p:commandButton value="Do stuff" action="#{testBean.doSomeLogic()}" process="@form" update="@form"/&gt; &lt;/f:facet&gt; &lt;/p:dataTable&gt; &lt;/h:form&gt; </code></pre> <p>Managed Bean:</p> <pre><code>@ManagedBean @ViewScoped public class TestBean implements Serializable { private List&lt;String&gt; list; public TestBean() { } @PostConstruct public void init() { list = new ArrayList&lt;String&gt;(); list.add("one"); list.add("two"); } public void addRow(){ list.add(new String()); } public void doSomeLogic(){ for (String string : list) { System.out.println(string); } } // getters and setters } </code></pre> <p>What actually happens: </p> <ul> <li>the user clicks on "add row" button, a new row is added (I need immediate to be true so no validation is done, those fields are part of a bigger form).</li> <li>the user clicks on "do stuff", the collection has the right size (with new rows) but the user's input in not taken into account (neither modification to pre exiting rows, nor new values in freshly added rows).</li> </ul> <p>What can I do to submit the new values too? I'm only beginning JSF and I'm not sure I'm already 100% getting it.</p> <p>Thanks for your help.</p> <p>Possible duplicates:</p> <ul> <li><a href="https://stackoverflow.com/questions/3635013/add-a-row-to-hdatatable-via-ajax-with-request-scoped-bean-without-losing-the-ro">Add a row to h:dataTable via AJAX with request-scoped bean without losing the row data</a></li> <li><a href="https://stackoverflow.com/questions/2278353/how-to-dynamically-add-a-row-in-a-table-in-jsf/2280157#2280157">How to Dynamically add a row in a table in JSF?</a></li> <li><a href="https://stackoverflow.com/questions/11092513/jsf-datatable-adding-and-removing-rows-clear-rows-values">JSF datatable: adding and removing rows clear rows values</a></li> </ul> <p><strong>Edit:</strong> problem is solved thanks to Jitesh, a working example can be found here: <a href="https://stackoverflow.com/questions/3409053/jsf2-can-i-add-jsf-components-dynamically?lq=1">JSF2, can I add JSF components dynamically?</a></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.
 

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