0
votes

I'm working with JSF and primefaces. I want to update a row when I click on the button (pencil). I used the first code from this tutorial (Primefaces docs), but dosent work in my case. the problem is : when i click the button (pencil) to update a row, nothing change. i have always the old values

This is my JSF code :

<f:view>
    <h:form id="form">
        <p:growl id="msgs" showDetail="true"/>


        <p:dataTable id="tservice" var="item" value="#{serviceMBean.services}" editable="true" style="margin-bottom:20px">
            <f:facet name="header"> edit </f:facet>
            <p:ajax event="rowEdit" listener="#{serviceMBean.onRowEdit}" update=":form:msgs" />
            <p:ajax event="rowEditCancel" listener="#{serviceMBean.onRowCancel}" update=":form:msgs" />
            <p:column headerText="Libelle du service">
                <p:cellEditor>
                    <f:facet name="output"><h:outputText value="#{item.libelleservice}" /></f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{item.libelleservice}" style="width:100%" label="Libelle"/>
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="Utilisateurs/Service">
                <p:cellEditor>
                    <f:facet name="output"><h:outputText value="#{item.nbruserservice}" /></f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{item.nbruserservice}" style="width:100%" label="Nbre Users"/>
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column style="width:32px">
                <p:rowEditor />

            </p:column>

And this is my managed bean code:

@Named(value = "serviceMBean")
@ViewScoped
public class ServiceMBean implements Serializable {

    private List<Service> serviceList;
    private Service s;

    @EJB
    private ServiceManager serviceManager;

    public ServiceMBean() {}

    public void modifierService(Service s1) {
        this.serviceManager.updateService(s1);
    }

    public void onRowEdit(RowEditEvent event) {
        Service s2 = (Service) event.getObject();
        System.out.println("--->" + s2.getNbruserservice());
        FacesMessage msg = new FacesMessage("Service Modifié", ((Service) event.getObject()).getLibelleservice());
        this.modifierService(s2);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void onRowCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Modification annulée", ((Service) event.getObject()).getLibelleservice());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

And this is my code for ServiceManager:

public void updateService(Object service) {
    em.merge(service);
}

I think the setter in my JSF page doesn't work. Any solutions or suggestions?

1
What doesn't work? What do you expect to happen, and what happens instead?Predrag Maric
when i click the button (pencil) to update a row, nothing change. i have always the old valuesredoff
Can you please change attribute in your updateService function to ServiceManager ? public void updateService(ServiceManager service) and tell me if it works ?Silwest
thank you for answer, but dosent work :(. Actually, ServiceManager it's my session class, and ServiceMBean it's a managedbeanredoff
The problem is in the jsf or setter, the jsf dont take the new values, because i made this example : public void onRowEdit(RowEditEvent event) { Service s2 = (Service)event.getObject(); System.out.println("--->"+s2.getNbruserservice()); s2.setNbreUserservice(5); // this the change FacesMessage msg = new FacesMessage("Service Modifié", ((Service) event.getObject()).getLibelleservice()); this.modifierService(s2); FacesContext.getCurrentInstance().addMessage(null, msg); } and the value 5 is persisted on database, so all the methods works fineredoff

1 Answers

0
votes

Solution : Add a void method with @PostConstruct.

Example : @PostConstruct

@PostConstruct
public void initialiser() {
    serviceList = this.getServices();
}
  1. Don't forget the getter and setter of serviceList
  2. In the JSF page, the value of datatable will be : value="#{ServiceMBean.serviceList}"