更新带有Primefaces Ajax的对话框? [英] Updating a dialog with ajax of primefaces?

查看:75
本文介绍了更新带有Primefaces Ajax的对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个对话框.对话框(列表)和对话框(创建).在对话框列表中,我有一个<p:dataTable/>,其中包含Dialog(create)插入的值. 对话框列表中,打开对话框创建"以插入值.

I have 2 dialogs. Dialog(list) and Dialog(create). On dialog list I have a <p:dataTable/> that contain values that are inserted of Dialog(create). Dialog list, open Dialog create to insert values.

我想要的是在dialog(create)中自动插入值后,在dialog(create)中插入值后刷新.

What I want is after insert values in dialog(create) automatically dialog(list) refresh with values inserted.

在寻找解决方案时,我发现了将<p:commandButton/>update与质素的ajax结合使用的任何建议.我正在尝试这样做,但仍然没有成功.

Looking for a solution, I found any suggestions to use update of <p:commandButton/> with ajax of primefaces. I am trying this but still haven't success.

在dialog(create)处插入值后,如何更新dialog(list)?

How I can do update dialog(list) after insert values at dialog(create) ?

这是我的尝试方式.

对话框(列表)

<p:dialog header="Turmas" id="TurmaViewDlg" widgetVar="TurmaViewDialog" appendTo="@(body)" modal="true" resizable="false" draggable="false">
    <h:form id="TurmaListForm">
        <p:panel id="display">
            <p:dataTable id="datalist" value="#{turmaMB.turmas}" var="turma" 
                                    paginator="true" 
                                    rows="5" 
                                    rowsPerPageTemplate="5,10,20,30,40,50"
                                    selectionMode="single"
                                    selection="#{turmaMB.bean}"
                                    rowKey="#{turma.id}"                        
                                    liveScroll="true"
                                    > 
                    <p:column headerText="#">
                            <h:outputText value="#{turma.id}"/>         
                    </p:column> 
                    <p:column headerText="Turma">
                            <h:outputText value="#{turma.turma}"/>
                    </p:column>
                    <p:column headerText="Ano">
                            <h:outputText value="#{turma.ano}" />
                    </p:column> 
            </p:dataTable>
            <p:commandButton id="createButton" icon="ui-icon-plus" value="Novo" update=":TurmaCreateForm" oncomplete="PF('TurmaCreateDialog').show()"/>
        </p:panel>
    </h:form>  

    <ui:include src="create.xhtml"/>
</p:dialog>

对话框(创建)

<p:dialog header="Cadastro de turma" id="TurmaCreateDlg" widgetVar="TurmaCreateDialog" appendTo="@(body)" modal="true" resizable="false" draggable="false">

    <h:form id="TurmaCreateForm">
        <p:growl id="growl" life="3000"/>
        <p:panelGrid columns="2" id="display">
            <f:validateBean>            
                <p:outputLabel value="Turma"/>
                    <p:inputText value="#{turmaMB.bean.turma}"                             
                             maxlength="50"
                             id="turmaId"
                             >                     
                </p:inputText>                

                <p:outputLabel value="Ano"/>
                    <p:inputText value="#{turmaMB.bean.ano}"                            
                             maxlength="4"
                             id="anoId"
                             >                         
                    </p:inputText>   
            </f:validateBean>                           
        </p:panelGrid>

        <p:panel>
            <p:commandButton value="Salvar" action="#{turmaMB.insert(turmaMB.bean)}" update="display,:TurmaListForm:datalist,growl">            
                <f:ajax execute="@form" render="@form"/>
            </p:commandButton>            
            <p:commandButton value="Cancelar" onclick="TurmaCreateDialog.hide()"/>
        </p:panel>


    </h:form>            
</p:dialog>

托管豆

@ManagedBean
@ViewScoped
public class TurmaMB {
    private Turma bean = new Turma();
    private GenericDAO<Turma> dao = new GenericDAO<Turma>(Turma.class);
    private List<Turma> turmas = null;

    public void insert(Turma t){        
        dao.insert(t);
        bean = new Turma();
    }



    public Turma getBean() {
        return bean;
    }



public List<Turma> getTurmas() {
    if(turmas == null){
        turmas = dao.findAll();
    }
    return turmas;
}

推荐答案

您的代码很好,而Ajax似乎不是问题.问题出在您的turmas列表中,该列表填充一次:第一次访问turmas时.

Your code is fine and Ajax doesn't seems to be the problem. The problem originates from your turmas List which is populated once : when turmas is accessed for the very first time.

所以您有两个选择.

  • 使用turmas=null清除turmas列表,因此强制dao在第一次调用getTurmas()时重新加载所有内容.
  • 将新的turma实体添加到turmas列表中.
  • clearing the turmas list with turmas=null and therefore force the dao to reload everything on the first call to getTurmas().
  • add the new turma entity to the turmas list.

当然,第一种选择是在数据库上生成更多流量.第二种选择是备用数据库,但是您在某种程度上拥有所有数据的较旧副本.

Of course the first option is going to generate more traffic on the database. The second option spare the database but you somewhat have an older copy of all the data.

因此,基本上所有这些解决方案都可以解决您的问题:

So basically any of these solutions should solve your problem :

选项1:

public void insert(Turma t){      
   dao.insert(t);
   turmas = null;
   bean = new Turma();
}

选项2:

public void insert(Turma t){      
   dao.insert(t);
   turmas.add(t);
   bean = new Turma();
}

这篇关于更新带有Primefaces Ajax的对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆