0

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:

enter image description here

Hello Lili
  • 1,527
  • 1
  • 25
  • 50
  • @BalusC I'm sorry but I didn't find the answer in the questions you suggested. I don't think those questions refer to the same problem. My problem is that, in my datatable, I can't send the selected value to the managed bean. – Hello Lili Dec 26 '17 at 11:41
  • @BalusC I would consider my question to be a duplicate of this one, actually (after searching and searching the internet): https://stackoverflow.com/questions/988869/how-can-i-get-values-out-of-a-selectonemenu-inside-a-jsf-datatable But weird, I follow the answer there exactly (using a binding), but still it doesn't work. – Hello Lili Dec 26 '17 at 14:09
  • You don't need to manually pass around submitted values. JSF already does that automatically for you when used correctly. – BalusC Dec 26 '17 at 19:47
  • After some research I used a binding in the dataTable and to get the table row I use Auction row = (Auction) dataTable.getRowData(); but it still doesn't work, it retrieves the default value for the select and not the selected value. – Hello Lili Dec 27 '17 at 12:37
  • @BalusC Thank you so much! After re-reading the second question you suggested me: "How and when should I load the model from database for h:dataTable" I figured it out!! I should have initialized the list of Auctions in a PostConstruct method! Thank you thank you thank you! – Hello Lili Dec 27 '17 at 13:06
  • It's answered in both duplicates. – BalusC Dec 27 '17 at 14:14

0 Answers0