在重复结构内捕捉PrimeFaces的微调器值 [英] Catching PrimeFaces's spinner value inside repeat structure

查看:44
本文介绍了在重复结构内捕捉PrimeFaces的微调器值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Primefaces和微调器组件.我的问题是,如果在迭代结构中,则未在bean中设置Spinner值.我的微调器位于ui:repeat中. 最后,问题是如何处理映射到bean中相同属性的不同表单控件.

I'm using Primefaces and spinner component. My problem is that the spinner value is not set in the bean, if it's inside an iteration structure. My spinner is inside ui:repeat. In the end, the problem is how to deal with different form controls mapping to the same property in bean.

<h:form>
    <ui:repeat var="item" value="#{myBean.items}">   
        <p:spinner size="2" min="1" max="50" style="width:75px" value="#{cartBean.quantityToOrder}"/>
        <p:commandButton value="Add to cart" action="#{cartBean.saveItemToCart(item)}" ajax="false"/>
    </ui:repeat>
</h:form>    

和我的豆子

@ManagedBean
@SessionScoped
public class CartBean extends BaseBean {

    private int quantityToOrder;

   //setter, getter...

   //When called quantityToOrder = 0 always
   public void saveItemToOrder(Item item) {
       quantityToOrder IS 0.
   }
}

我怀疑这与表单提交有关,我尝试了将集合中的所有元素都包含在内的表单,也将其中的任何Spinners +按钮都包含了其中.对于所有微调器,生成的客户端ID都是不同的.

I suspect it has to do with form submission, I have tried a form enclosing all elements in the collection and also a form enclosing any of the spinners + button. The generated client IDs are distinct for all spinners.

任何帮助将不胜感激.

推荐答案

在您的setQuantityToOrder(int quantityToOrder)方法上放置一个System.out.println("quantity: " + quantityToOrder),将看到此问题.最后一个微调器的值将优先于其他微调器,因为所有微调器都指向相同的属性(cartBean.quantityToOrder).

Put a System.out.println("quantity: " + quantityToOrder) on your setQuantityToOrder(int quantityToOrder) method and will will see the problem. The value of the last spinner will prevail over the others because all the spinners are pointed to the same property (cartBean.quantityToOrder).

尝试按以下方式将quantityToOrder移至项目:

Try moving the quantityToOrder to the Item as follows:

<h:form id="mainForm">
    <ui:repeat value="#{cartBean.items}" var="item">
        <p:outputLabel value="#{item.name}: " for="sp" />
        <p:spinner id="sp" size="2" min="1" max="50" style="width:75px" value="#{item.quantityToOrder}" />
        <p:commandButton value="Add to cart" action="#{cartBean.saveItemToOrder(item)}" process="@this, sp" update=":mainForm:total" />
        <br />
    </ui:repeat>
    Total: <h:outputText id="total" value="#{cartBean.quantityToOrder}" />
</h:form>

cartBean:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class CartBean implements Serializable {

    private List<CartItem> items;

    private int quantityToOrder;

    @PostConstruct
    public void setup() {
        items = new ArrayList<CartItem>();
        items.add(new CartItem(1, "A"));
        items.add(new CartItem(2, "B"));
        items.add(new CartItem(3, "C"));
    }

    public void saveItemToOrder(CartItem item) {
        //do whatever you want to do with the item quantity.
        System.out.println("Qtd of " + item.getName() + ": " + item.getQuantityToOrder());

        //to calculte the qtd of items on the cart.
        quantityToOrder = 0;
        for (CartItem cartItem : items) {
            quantityToOrder += cartItem.getQuantityToOrder();
        }
    }

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public int getQuantityToOrder() {
        return quantityToOrder;
    }

    public void setQuantityToOrder(int quantityToOrder) {
        this.quantityToOrder = quantityToOrder;
    }

}

CartItem:

import java.io.Serializable;

public class CartItem implements Serializable {

    private Integer id;

    private Integer quantityToOrder;

    private String name;

    public CartItem(Integer id, String name) {
        this.id = id;
        this.name = name;
        quantityToOrder = 0;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getQuantityToOrder() {
        return quantityToOrder;
    }

    public void setQuantityToOrder(Integer quantityToOrder) {
        this.quantityToOrder = quantityToOrder;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CartItem other = (CartItem) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

}

这篇关于在重复结构内捕捉PrimeFaces的微调器值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆