How to update Primefaces datatable every time when page is downloaded?

£可爱£侵袭症+ 提交于 2020-01-06 07:56:16

问题


I just have <p:dataTable/>. When I add a new row to that table in another page, the table is not refreshed. RmaBean is managedBean scoped session. Any ideas?

<h:form id="mainMenuForm">        

            <p:dataTable id="dataTable" 
                         var="case" 
                         value="#{RmaBean.rmaModel}" 
                         widgetVar="rmaTable" 
                         editable="true" 

                         rows="10" paginator="TRUE" emptyMessage="No items found with given criteria.">

...

<p:column></p:column>
        <p:column></p:column>
        <p:column><p:commandButton action="#{MainController.talletaUusiRma}" value="#{bundle.newRma_tallenna}" immediate="true"  ajax="false" style="width: 220px;"/></p:column>
    </p:row>

When I add a new object to that table, I just update the rmaModel every time and I even check that the list is one longer than before and there is that new object. The dataTable shows still the old data without the new object, why?

--------CREATING NEW RMA-------------

public String prepareNewRma() {
    current = new Rma();
    current.setUser("");
    return "newRma";
}

--------FETCHING DATA TO THE NEW RMA WITH SERIAL NUMBER------

public String haeTiedotSarjanrolla() {
    log.info("sarjanro=" + current.getSarjanro());
    System.out.println("---------------->sarjanro=" + current.getSarjanro());

    Sarjanumerot s = helper.getSerialInfo(current.getSarjanro());

    if (s != null) {
        current.setAn8(s.getAn8());
        current.setDsc1(s.getDsc1());
        current.setDsc2(s.getDsc2());
        current.setSarjanro(s.getId().getLotn());
        current.setTuotenro(s.getId().getLitm());
    }
    return "newRma";
}

--------SAVING NEW RMA---------------

public String talletaUusiRma() {

    current.setCreated(new Timestamp(System.currentTimeMillis()));
    helper.tallennaRma(current);
    rmaNumbers = null;

   RmaBean papu = (RmaBean) JsfUtil.findBean("rmapapu");

   papu.updateTable();
    return "mainMenu";
}

HELPER:

 public void tallennaRma(Rma current) {
        try {
            Session session = HibernateUtil.getAsekorjausSessionFactory().getCurrentSession();
            session.beginTransaction();
            session.saveOrUpdate(current);
            session.getTransaction().commit();
        } catch (Exception e) {
            log.debug(e.getMessage());
                        JsfUtil.addErrorMessage(ResourceBundle.getBundle("resources/Bundle").getString("editRma_err_updateError"));
        }

---------UPDATING MODEL AND TABLE in RmaBean-----------

public void updateTable(){

    System.out.println("updateTableupdateTableupdateTable");
    rmaModel.setWrappedData(rmaModel.getUpdatedList());
    System.out.println("1111111..........");
    List<Rma> rmat = rmaModel.getUpdatedList();
    System.out.println("22222222.......");
    Iterator iter = rmat.iterator();
    System.out.println("------------------------>PITUUS: " +rmat.size());
    while (iter.hasNext()) {
        Rma t = (Rma) iter.next();
        System.out.println("t.getRma()NUMERO------->" +t.getRma());


    }

    RmaDataModel newRMAModel = new RmaDataModel(rmat); 
    rmaModel = newRMAModel;
}

Sami


回答1:


value-attribute of you dataTable is in session scope, so your datatable will only be rendered on you first request in this session (if you do not update this view manually).

Use view scoped bean for your views. If it's necessary that this list is in session scope, just inject the list from any session scoped bean to your view scoped bean.




回答2:


If I understand well your issue, you want to update one page when the action is done in another page, that is not automatically done with the ajax update. It has to be done with push. You have a few examples of it in the primefaces showcase:

http://www.primefaces.org/showcase/push/index.jsf

From the first example:

<h:form id="form">  
    <h:outputText id="out" value="#{globalCounter.count}" styleClass="ui-widget display" />  

    <br />  

    <p:commandButton value="Click" actionListener="#{globalCounter.increment}" />  
</h:form>  

  <p:socket onMessage="handleMessage" channel="/counter" /> 

bb

   public synchronized void increment() {  
        count++;  

        PushContext pushContext = PushContextFactory.getDefault();  
        pushContext.push("/counter", String.valueOf(count));  
    }  



回答3:


I made a real bubblegum solution because I couldn't fit it otherwise. I just write bean again to session, so it goes over the old one with name "rmapapu". No it is working. Is there any consequences or is this really bad idea?

    rmaModel = null;
    RmaDataModel rmaModel = new RmaDataModel(rmat);
    //rmaModel = newRMAModel;
    this.setRmaModel(rmaModel);

    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
    HttpSession httpSession = request.getSession(false);
    httpSession.setAttribute("rmapapu", this);

Thanks for everybody helping me. I still will read BalusC tutorial about that, because it would be nice to know how you should do that :)

Sami



来源:https://stackoverflow.com/questions/13242449/how-to-update-primefaces-datatable-every-time-when-page-is-downloaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!