Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a4j:support - Value retrieved from h:selectOneMenu is always NULL

There's a datatable with a h:selectOneMenu in each row. I want to be able to retrieve the value selected in the selectOneMenu in the bean. I'm using richfaces a4j:support tag to make AJAX calls to the backing bean. You can see the code below:

DataTable header:

<t:dataTable id="datatable" var="row" value="#{myBean.dataTableRows}">

SelectOneMenu with A4j:

<h:selectOneMenu id="type" label="Type:" styleClass="tamanho80" 
                                value="#{datatableHolder.selectedValue}" converter="comboConverter" immediate="true" >                           
  <f:selectItem itemValue="#{null}" itemLabel="" />
  <t:selectItems var="tp" 
    itemValue="#{tp}" itemLabel="#{tp.nome}"
        value="#{row.comboTypeValues}"  />
   <f:attribute name="row" value="#{row}"/>                                                         
   <a4j:support event="onchange" reRender="parent" actionListener="${myBean.executeAjax}" immediate="true" ajaxSingle="true" />
</h:selectOneMenu>

The Backing Bean method to be executed:

public void executeAjax(ActionEvent event){

    ValueHolder comboBox = (ValueHolder) event.getComponent().getParent();      
    comboBox .getValue();

}
  • comboBox .getValue() is returning NULL, even when I select a value.

PS:

This question has been identified as a possible duplicated of this question, but it's not. My question uses a dataTable and doesn't use binding for each element. Also, I'm using JSF 1.1 and RichFaces 3.3.3.

like image 654
jguilhermemv Avatar asked Sep 27 '22 15:09

jguilhermemv


1 Answers

The problem was identified:

Each "option" generated by the t:selectItems tag was with the item id instead of an index, while the comboConverter was using an index to select the item. So, the list had 12 items (indexes should range from 0 to 11), but the id of the selected item was 22 for example. Then the converter would traverse the list to the index 22 and retrieve the element. However there's not such index in this list, because the max value is 12 and then the converter would always return NULL.

For this problem there are basically 3 ways to solve:

  • Create a new converter that look for the item by it's id
  • Adapt/Change the "comboConverter" to look for the item by it's id (doing that so, would impact other pieces of code that use this converter
  • Adapt the list to use indexes instead of ids

I've chosen the first one, due to the minor impact in the system.

like image 93
jguilhermemv Avatar answered Oct 01 '22 02:10

jguilhermemv