0

我有一个 primefaces 数据表。我从数据库中填充它。其中一个字段是由复选框表示的布尔值。我希望如果我选中或取消选中该复选框,我可以将更改保存回数据库。

我尝试将行的当前值传递给托管 bean 进行保存,但复选框的新值未反映在当前行对象中。如何将更改放入当前行对象,以便成功将更改保存到数据库?

这就是我现在正在做的事情......我试图提供所需要的。如果信息太多或太少,请告诉我。谢谢。

@ManagedBean(name = "itemManagerBean")
@ViewScoped
public class ItemManagerBean implements Serializable {
...
    public ArrayList<Item> getAllItemsForUser() {
        List list = ecf.findByPartyId(user.getPartyId());
        ArrayList<Item> itemList = new ArrayList<>(list);
        return (itemList);
    }
...
    public String saveItem(Item item){
        System.out.println(item.toString());
        ecf.updateRecord(item);
        return (null);
    }
}

//item class 
public class Item {
    private BigInteger itemId;
    private String name;
    priave boolean saleable;   //database column is not null

    //getters and setters
}


//facelet
<h:form>
<p:dataTable id="id_itemList"
             var="item" 
             value="#{itemManagerBean.allItemsForUser}" > 

    <p:column headerText="ID">  
        <h:outputText value="#{item.itemId}" />  
    </p:column>  

    <p:column headerText="Name">  
        <h:outputText value="#{item.name}" />  
    </p:column>  

    <p:column headerText="Saleable" >  
        <p:selectBooleanCheckbox value="#{item.saleable}" />
    </p:column>  

    <p:column  width="15" > 
        <p:commandButton id="id_saveRowButton" icon="ui-icon-disk" 
             title="Save" action="#{itemManagerBean.saveItem(item)}"/>  
    </p:column>  
</p:dataTable>
</h:form>
4

1 回答 1

1

You need to create a selectedItem property in ItemManagerBean and update its value when the user clicks on the commandButton:

In ItemManagerBean

private Item selectedItem;
// getter and setter

In the xhtml page

<p:column  width="15" > 
    <p:commandButton id="id_saveRowButton" icon="ui-icon-disk" 
         title="Save" action="#{itemManagerBean.saveItem}">
        <f:setPropertyActionListener value="#{item}" target="#{itemManagerBean.selectedItem}" /> 
    </p:commandButton> 
</p:column> 

(Note that you don't need to pass item through saveItem method. Modify saveItem in the managed bean in order to make it work with selectedItem instead of accepting an input item).

Links:

example in the PrimeFaces showcase

Passing parameter to JSF action

BalusC blog

于 2012-09-28T10:03:11.370 回答