0

I have a primefaces datatable. I populate it from the database. One of the fields is a boolean represented by a checkbox. I want that if I check or uncheck the checkbox, that I can save the change back to the database.

I have tried passing the current value of the row to the managed bean to save, but the new value of the checkbox isn't reflected in the current row object. How can I get the change into the current row object so I can successfully save the change to the DB?

Here is what I am doing now... I have tried to provide just what is needed. If it is too much information or too little, let me know. Thanks.

@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>
Bill Rosmus
  • 2,941
  • 7
  • 40
  • 61

1 Answers1

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

Community
  • 1
  • 1
perissf
  • 15,979
  • 14
  • 80
  • 117