I hava a dataTable in a jsf, how can i get all the values from that table.
Here is my table:
<h:dataTable id="dt1" value="#{Metadata.placeholders}" var="item" binding="#{Metadata.dataTable}" bgcolor="#F1F1F1" border="10" cellpadding="5" cellspacing="3" first="0" rows="4" width="40%" frame="hsides" rules="all" summary="This is a JSF code to create dataTable." >
<f:facet name="header">
<h:outputText value="Select elements for available placeholder" />
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="Placeholder" />
</f:facet>
<h:outputText value="#{item.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Element"/>
</f:facet>
<h:selectOneListbox id="elements" size="1" >
<f:selectItems value="#{item.elements}" />
</h:selectOneListbox>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Value"/>
</f:facet>
<h:inputText></h:inputText>
</h:column>
</h:dataTable><br>
As you can see, the second and third column are listbox and input text felds, so let assume that the table has 5 rows and the user select a value form the listbox and input a text in the text field, so how can i get that data after the user press "submit" button?
Advertisements. JSF provides a rich control named DataTable to render and format html tables. DataTable can iterate over a collection or array of values to display data. DataTable provides attributes to modify its data in an easy way.
JSF <h:dataTable> Tag. It is used to create a data table.
Just bind the dropdown value to a property of the currently iterated item.
<h:dataTable value="#{metadata.placeholders}" var="placeholder">
<h:column>
<h:selectOneMenu value="#{placeholder.element}">
<f:selectItems value="#{placeholder.elements}" />
</h:selectOneMenu>
</h:column>
</h:dataTable>
<h:commandButton value="Submit" action="#{metadata.submit}" />
(note that I fixed the nonsensicial <h:selectOneListbox size="1">
by a <h:selectOneMenu>
)
When you submit the form, JSF will just set the value in the element
property of the iterated Placeholder
object. If you intend to access it individually just loop over placeholders
in the action method.
public void submit() {
for (Placeholder placeholder : placeholders) {
System.out.println(placeholder.getElement()); // Look, JSF has already set it.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With