adgfs
adgfs

Reputation: 423

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?

Upvotes: 6

Views: 4936

Answers (3)

BalusC
BalusC

Reputation: 1108632

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.
    }
}

Upvotes: 5

Grim
Grim

Reputation: 1974

Your Listbox and your Inputtext does not know where (and how) to store their values. So JSF usualy have managed-bean's and converter's and map the values via value-attribute.

If you are not sure whats the name of the properties, you can inherit two hashmaps to pass the values. eg:

< ... value="#{Metadata.placeholderMap[item.id]['age']}" ... >

Upvotes: 1

user219882
user219882

Reputation: 15834

It's just a sample code, but the idea should be in there

for (int i = 0; i < getDataTable().getRowCount(); i++) {
    getDataTable().setRowIndex(i);
    Object object = getDataTable().getRowData();
}

Upvotes: 2

Related Questions