Later edit: The question How and when should I load the model from database for h:dataTable solves my problem!
Be careful to intialize your list of items correctly when using a dataTable!
In a table containing auctions, I'm trying to update an auction's state - which is selected using a selectOneMenu
element.
The problem is the selected value is not updated - I keep getting the old value of the auction on the associated row after submitting the form.
How can I send the selected value as a parameter for changeState()?
auctions.xhtml
<h:dataTable value="#{auctionService.getAuctions()}" var="auction" class="table table-striped">
<h:column>
<f:facet name="header">Activ/inactiv</f:facet>
<h:selectOneMenu value="#{auction.active}" class="form-control" >
<f:selectItems value="#{auction.valuesActive}" />
</h:selectOneMenu>
<h:column>
<f:facet name="header">Modifica stare</f:facet>
<h:commandButton action="#{auctionService.changeState(auction, auction.active)}" value="Modifica stare" class="btn btn-primary"></h:commandButton>
</h:column>
</h:column>
</h:dataTable>
</h:form>
The important code in the Auction bean:
@ManagedBean(name="auction", eager=true)
@RequestScoped
@Entity
public class Auction implements Serializable{
/**
*
*/
private int active; //plus getters and setters which I did not copy here
@Transient
private List<SelectItem> valoriActiv = new ArrayList<SelectItem>(Arrays.asList(new SelectItem(0), new SelectItem(1))); //plus getters and setters which I did not copy here
The auctionService bean contains this important method:
public String changeState(Auction l, int activ){
auctionsDAO.changeState(l, activ);
return "auctions.xhtml";
}
The corresponding code in auctionsDAO service class (just a class which takes care of the operations with the database - I'm using JPA for database access):
public void changeState(Auction licitatie, int activ){
System.out.println("in metoda");
EntityManager em = factory.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Auction auctionHelper = em.find(Auction.class, licitatie.getAuctionID());
auctionHelper.setActiv(activ);
tx.commit();
em.close();
}
I'm also posting an image with what I want to achieve, in case I didn't explain well enough: