~ shopping cart ammount change working without database

This commit is contained in:
Johannes Theiner 2019-06-06 17:10:04 +02:00
parent cc06ec797e
commit 7d443a1aeb
2 changed files with 23 additions and 3 deletions

View File

@ -8,6 +8,7 @@ import javax.faces.annotation.ManagedProperty;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
@ -35,6 +36,9 @@ public class CartBean {
return (Address) context.getApplication().evaluateExpressionGet(context, "#{addressBean.selected}", Address.class);
}
@Setter
private List<ShoppingCartItem> items;
public String add(Book book) {
if (getUser() != null && getUser().getId() != 0) {
for (ShoppingCartItem item : getItems()) {
@ -58,11 +62,20 @@ public class CartBean {
}
public List<ShoppingCartItem> getItems() {
if(items != null) return items;
Database db = Database.getInstance();
if (getUser() != null) {
return db.shoppingCart().get(getUser());
items = db.shoppingCart().get(getUser());
} else {
return new ArrayList<ShoppingCartItem>();
items = new ArrayList<ShoppingCartItem>();
}
return items;
}
public void change(ValueChangeEvent event) {
System.out.println("änderung");
for(ShoppingCartItem item : getItems()) {
System.out.println(item.getCount());
}
}

View File

@ -38,7 +38,14 @@
</div>
<div class="m-row">
<div class="m-col-t-4 m-push-t-6">
<input type="number" name="quantity" value="#{item.count}" min="1" max="10"/>
<h:form>
<h:inputText value="#{item.count}" valueChangeListener="#{cartBean.change}">
<f:validateLength minimum="1" maximum="3"/>
<f:ajax />
</h:inputText>
</h:form>
<h:messages globalOnly="false"/>
</div>
</div>
<div class="m-row">