0

当我单击调用用于过滤选定项目的方法的按钮时,即使选中了它的复选框,每个选定的项目参数都是错误的。这就像 selectBoolean 复选框的值总是假的。我没有意识到这个问题。为什么不将值设置为true?

我有一个带有数据表的 JSF 页面:

<h:dataTable value="#{productManagedBean.showProducts()}" var="item">
   <h:column>
      <f:facet name="header">
         <h:outputText value="Select"/>
      </f:facet>
      <h:selectBooleanCheckbox value="#{item.selected}"></h:selectBooleanCheckbox>
   </h:column>
<h:column>
   <f:facet name="header">
      <h:outputText value="Image"/>
   </f:facet>
   <div class="container-images-product-list">
      <h:graphicImage value='#{item.product.image}' class="images-product-list" />
   </div>

<h:form>
   <h:commandButton value="Delete selected product/s" class="pure-button pure-button-primary" action="#{productManagedBean.filterCheckedItems()}"></h:commandButton>
</h:form>

dataTable 中包含的项目类别:

public class ProductSelection {
    private Product product;
    private boolean selected;

    public ProductSelection(Product product) {
        this.product = product;
    }

    public ProductSelection(Product product, boolean selected) {
        this.product = product;
        this.selected = selected;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

ManagedBean 中的方法:

public List<ProductSelection> showProducts(){
    if(itemsShown == false) {
        list = productSessionBean.getAllProducts();
        for(Product p: list){
            selectionList.add(new ProductSelection(p));
        }
        itemsShown = true;
    }
    for (ProductSelection p: selectionList) {
        p.getProduct().setImage("../uploaded/" + p.getProduct().getImage());
    }
    return selectionList;   
}

public void filterCheckedItems(){
    for (ProductSelection p: selectionList) {
        if(p.isSelected()){
            checkedList.add(p.getProduct());
        }
    }
}
4

1 回答 1

1

请把dataTable和commandButton放在同一个表格里,然后再试一次。目前,您的复选框选择似乎未提交,因为 dataTable 未嵌套在同一表单中。

于 2014-09-11T07:27:15.540 回答