Search code examples
jsfmaster-detailajax-update

Data list does not refresh after editing


I would like to refresh the datalist after editing an element. I have used @Postconstruct in order to initialize the data, but in vain.

The following code display a data list:

<h:form>

  <p:dataTable widgetVar="developpeur" value="#{manageUrlSiteTestBean.sitewebs}" var="dev"   style="margin-bottom:20px">
 <f:facet name="header">
Les tests de la website url </f:facet>

   <p:column headerText="titre">
 #{dev.title}
 </p:column>


 <p:column>
 <p:commandButton value="edit"  update=":form">
  <f:setPropertyActionListener target="#{manageUrlSiteTestBean.siteweb}" value="#{dev}"></f:setPropertyActionListener>
   <f:setPropertyActionListener target="#{manageUrlSiteTestBean.form}" value="true"></f:setPropertyActionListener>
 </p:commandButton>
 </p:column>
 </p:dataTable>
 </h:form>

The second part of the code is a form that retrieves data selected for editing

<h:panelGrid id="form">

 <h:form rendered="#{manageUrlSiteTestBean.form}" >
 <p:panel header="Modification">
 <h:panelGrid columns="1">

 <p:outputLabel value="Name" id="lcin"/>
 <p:inputText id="cink" value="#{manageUrlSiteTestBean.siteweb.title}" required="true" >
 </p:inputText>

  </h:panelGrid>
 <p:commandButton value="Validate" action="#{manageUrlSiteTestBean.validate()}" />
  </p:panel>
  </h:form>
 </h:panelGrid> 

And the following code is the backing bean

@ManagedBean
@ViewScoped
public class ManageUrlSiteTestBean implements Serializable{

    public ManageUrlSiteTestBean(){

    }
    private static final long serialVersionUID = 1L;
    @EJB
    ManageUrlSiteTestLocal local;
    private SiteWebImpose siteweb = new SiteWebImpose() ;
    private List<SiteWeb> sitewebs=new ArrayList<SiteWeb>();
    private boolean form;
    public ManageUrlSiteTestLocal getLocal() {
        return local;
    }
    public void setLocal(ManageUrlSiteTestLocal local) {
        this.local = local;
    }
    public List<SiteWeb> getSitewebs() {
        return sitewebs;
    }
    public void setSitewebs(List<SiteWeb> siteweb) {
        this.sitewebs = siteweb;
    }
    public SiteWebImpose getSiteweb() {
        return siteweb;
    }
    public void setSiteweb(SiteWebImpose siteweb) {
        this.siteweb = siteweb;
    }   
    public boolean isForm() {
        System.out.println("nn");
        return form;
    }
    public String validate(){
        local.updateUrlSite(siteweb);
        form=false;
        siteweb=new SiteWebImpose() ;
        init();
        return null;        
    }
    public void setForm(boolean form) {
        this.form = form;
    }   
    @PostConstruct
    public void init(){
        setSitewebs(local.listerUrlSiteTests());
    }
}

How can I achieve this?


Solution

  • you dont actually update your list when you trigger your button's validate action. so the list will be updated, ok, but you need to ajax update it at the html too. try sth like this

    <p:commandButton value="Validate" action="#{manageUrlSiteTestBean.validate()}" update=":form:developpeur"/>
    

    i dont remember if you have to declare also an id for your datatable or it will work with just the widgetVar but i think you need an id too, so put id="developpeur" at your dataTable too. and add id="form" at your first form

    have a look at the examples here about ajax submit and non-ajax submit

    http://www.primefaces.org/showcase/ui/button/commandButton.xhtml