user1765988
user1765988

Reputation: 33

h:selectOneMenu in p:dataTable doesn't submit its value

I have a question about selectOneMenu and settting the values. I have an Object SampleDesc that has and ID, Text, and a List<SampleDescValues>. For each datatable row the Text is the output label and the select one menu values are the List<SampleDescValues>. XHTML:

    <h:panelGroup id="tables">
    <p:dataTable resizableColumns="true" 
             var="sampleDesc" id="SampleDescTable" rowIndexVar="rowIndex"
                     value="#{sampleBean.sampleDescList.list}" 
                     rendered="#{sampleBean.sampleDescList.list.size() gt 0}">
            <p:column>
                    <h:outputLabel value="#{sampleDesc.sampleDescText}"/>
                </p:column>
        <p:column>
            <h:selectOneMenu required="#{sampleBean.sampleDescList.list.size() gt 0}" converter="#{sampleDescValueConverter}" 
                                                         id="SampleDescValue" value="#{sampleBean.selectedSampleDescList.get(rowIndex)}">                                                         
                <f:selectItem itemLabel="Select One" itemValue="#{null}"/>
                            <f:selectItems value="#{sampleDesc.sampleDescValues}" var="sdv" 
                                       itemLabel="#{sdv.sampleDescValuesText}" itemValue="#{sdv}" />

                        </h:selectOneMenu>
                </p:column>    
        </p:dataTable>
</h:panelGroup>   

I have the converter setup and it works because ive set it to a single SampleDescValue and it set the value.

The problem is when i try and populate the form with a Sample from the database it can only set one of the dropdowns when there could be an infinite number of selectonemenu's

I set the value selected to private List<SampleDescValue> selectedSampleDescList;

When i try and submit it does nothing, it works when the datatable is not rendered.

Upvotes: 3

Views: 2284

Answers (1)

BalusC
BalusC

Reputation: 1109635

Your menu value is wrong:

<h:selectOneMenu value="#{sampleBean.selectedSampleDescList.get(rowIndex)}">

It's not possible to perform a set operation on this EL expression.

Use the brace notation instead:

<h:selectOneMenu value="#{sampleBean.selectedSampleDescList[rowIndex]}">

Note that this expects a non-null selectedSampleDescList. So make sure that you've already properly initialized it with a new ArrayList<>() beforehand. EL won't do that for you. It will only set the list items using List#add(index, object) method.

See also:


Unrelated to the concrete problem, this expression

#{sampleBean.sampleDescList.list.size() gt 0}

can be simplified as follows

#{not empty sampleBean.sampleDescList.list}

And this is unnecessary in the required attribute of the <h:selectOneMenu> as it would always evaluate true at that point. Just use required="true" directly instead.

Upvotes: 2

Related Questions