How to get the value of an input component in the same data row as a button?

I have a datatable where the rows are dynamic and each row contains selectOneMenu. If I have a button in each row and I want to get the selected item on selectOneMenu, what is the best way to do this?

+1
source share
1 answer

Wrap the collection behind datatable value's DataModel<E>.

private List<Item> items;
private DataModel<Item> model;  // +getter

@PostConstruct
public void init() {
    this.items = loadItSomehow();
    this.model = new ListDataModel<Item>(items);
}

( Itemin this example, this is just the javabean class representing each row, for example Person, Productetc.)

Instead, bind it to a datatable value.

<h:dataTable value="#{bean.model}" var="item">

If the drop-down menu is tied to a property Item, and the button is tied to a method of the same bean ...

<h:column>
    <h:selectOneMenu value="#{item.value}">
        <f:selectItems value="#{bean.values}" />
    </h:selectOneMenu>
</h:column>
<h:column>
    <h:commandButton value="submit" action="#{bean.submit}" />
</h:column>

... DataModel#getRowData() :

public void submit() {
    Item item = model.getRowData();
    String value = item.getValue();
    // ...
}
+4

All Articles