S
S
SislaOpir2018-07-14 20:12:06
Java
SislaOpir, 2018-07-14 20:12:06

How to put values ​​from datatable(jsf) into collection?

There is a datatable on a jsf page whose columns are the name of the product, its price and the input field of the quantity of this product. When the button is clicked, it is necessary to load elements from the datatable into the collection where the value of the entered quantity is greater than 0 (the addCups method). How it is possible to implement it correctly? Thanks in advance.

@ManagedBean
@SessionScoped
public class OrderBean extends SpringBeanAutowiringSupport {
@Autowired
private OrderDAO orderDAO;

@Autowired
private OrderPositionDAO orderPositionDAO;


private Map<Long, Integer> selectedItems = new HashMap<>();

private Integer quantityOfCups;

public Map<Long, Integer> getSelectedItems() {
    return selectedItems;
}

public void setSelectedItems(Map<Long, Integer> selectedItems) {
    this.selectedItems = selectedItems;
}

public Integer getQuantityOfCups() {
    return quantityOfCups;
}

public void setQuantityOfCups(Integer quantityOfCups) {
    this.quantityOfCups = quantityOfCups;
}


public void addCups(Long id, Integer numOfCups){
    if(numOfCups > 0){
        selectedItems.put(id, numOfCups);
    }
    if(numOfCups == 0){
        selectedItems.remove(id);
    }
    System.out.println(selectedItems);
}

jsf page
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">

<h:form>
<p:dataTable value="#{coffeeBean.allCoffee}" var="coffee">
    <p:column>
        <h:outputText value="#{coffee.coffeeName}"/>
    </p:column>
    <p:column>
        <h:outputText value="#{coffee.costForCup}"/>
    </p:column>
    <p:column>
        <p:inputText value="#{orderBean.quantityOfCups}"/>
    </p:column>
</p:dataTable>
    <h:commandButton value="add cups" action="#{orderBean.addCups(coffee.id, orderBean.quantityOfCups)}"/>
</h:form>


</html>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question