Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable in JSF

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?

like image 611
adgfs Avatar asked Aug 30 '11 14:08

adgfs


People also ask

What is DataTable in JSF?

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.

Which JSF component can be used to create table?

JSF <h:dataTable> Tag. It is used to create a data table.


1 Answers

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.
    }
}
like image 86
BalusC Avatar answered Sep 28 '22 08:09

BalusC