Reputation: 4460
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.
What I want is after insert values in dialog(create) automatically dialog(list) refresh with values inserted.
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.
How I can do update dialog(list) after insert values at dialog(create) ?
here how I trying.
Dialog(list)
<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>
Dialog(create)
<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>
Managed bean
@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;
}
Upvotes: 1
Views: 1185
Reputation: 3618
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.
So you have two options.
turmas=null
and therefore force the dao to reload everything on the first call to getTurmas()
.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 :
Option 1:
public void insert(Turma t){
dao.insert(t);
turmas = null;
bean = new Turma();
}
Option 2:
public void insert(Turma t){
dao.insert(t);
turmas.add(t);
bean = new Turma();
}
Upvotes: 1