I have to implement the search the functionality in primefaces dialog. After submitiing the search commandbutton the table beside the search needs to get updated. But everytime i am hitting the search button the popup gets closed. Please guide.Below is my code snippet for reference:
......
<p:commandButton id="search" value="Search" actionListener="#{createTicketBaseBean.searchUserFromList}" update="#form" onclick="dlg.show()"></p:commandButton>
</h:panelGrid>
</h:panelGroup>
<h:panelGroup style="float:right" >
<p:dataTable id="table" var="user" value="#{createTicketBaseBean.userList}" selection="#{createTicketBaseBean.selectedUser}" selectionMode="single" rowKey="#{user.email}" >
<p:column headerText="Name" >
<p:commandLink id="nameselect" value="#{user.name}" onclick="dlg.hide()"/>
</p:column>
<p:column headerText="Email">
<h:outputText value="#{user.email}" />
</p:column>
<p:column headerText="Department">
<h:outputText value="#{user.department}" />
</p:column>
</p:dataTable>
Replace onclick in your p:commandbutton with oncomplete. onclick event is happening immediate when button is clicked. As this is AJAX button (in Primefaces this is default) there will be race conditioning between AJAX request and dialog opening. When using oncomplete it will be called after AJAX request is completed.
Related
I have a app created by JSF using Primefaces templates in Netbeans.
One of the pages is a Create.xhtml to insert new record in a mysql database. This page is called by a button in a footer of a List od records.
The problem that when I select this page, the form is empty, only have title and buttons. The outputLabel and inputText are "hidden" (inspecting the page in browser). But if I select first a record in the List and next the Create page the outputLabel and inputText already are visible, but with values.
I want that when I select the Create page this came in an empty form and when I want insert the values I wanted in database.
The code of List.xhtml:
<h:form id="DistritoCreateForm">
<p:panel header="#{bundle.CreateDistritoTitle}">
<h:panelGroup id="display">
<p:panelGrid columns="2" rendered="#{distritoController.selected != null}">
<p:outputLabel value="#{bundle.CreateDistritoLabel_nomeDistrito}" for="nomeDistrito" />
<p:inputText id="nomeDistrito" value="#{distritoController.selected.nomeDistrito}" title="#{bundle.CreateDistritoTitle_nomeDistrito}" required = "true" />
</p:panelGrid>
<p:commandButton actionListener="#{distritoController.create}" value="#{bundle.Save}" update="display,:growl" oncomplete="handleSubmit(args,'DistritoCreateForm');" action="List.xhtml"/>
<p:commandButton value="#{bundle.Cancel}" action="List.xhtml"/>
</h:panelGroup>
</p:panel>
</h:form>
The code of Create.xhtml:
<h:form id="DistritoListForm">
<p:panel header="#{bundle.ListDistritoTitle}">
<p:dataTable id="datalist" value="#{distritoController.items}" var="item" style="width:50%;"
selectionMode="single" selection="#{distritoController.selected}"
paginator="true"
paginatorPosition="bottom"
rowKey="#{item.id}"
rows="10">
<p:ajax event="rowSelect" update="createButton viewButton editButton deleteButton"/>
<p:column filterBy="#{item.nomeDistrito}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="#{bundle.ListDistritoTitle_nomeDistrito}"/>
</f:facet>
<h:outputText value="#{item.nomeDistrito}"/>
</p:column>
<f:facet name="footer">
<p:commandButton id="createButton" icon="ui-icon-plus" actionListener="#{distritoController.prepareCreate}" action="Create.xhtml"/>
<p:commandButton id="viewButton" icon="ui-icon-search" action="View.xhtml"/>
<p:commandButton id="editButton" icon="ui-icon-pencil" action="Edit.xhtml"/>
<p:commandButton id="deleteButton" icon="ui-icon-trash" actionListener="#{distritoController.destroy}" update=":growl,datalist" disabled="#{empty distritoController.selected}"/>
</f:facet>
</p:dataTable>
</p:panel>
</h:form>
So effectively (it cannot be seen in your code since it is not an mcve)
You select something
As a consequence a certain variable is set to be used in the shared 'show'/'edit'/'create' page/dialog
Next you press the create button and the same page/dialog is shown with the content from the selection before.
A simple deduction then is that the 'selected' variable is not cleared in the method that is called when the create button is pressed (#{distritoController.prepareCreate})
Solution:
Clear the variable (e.g. by creating a new empty instance of the object...
Effectively this is not a PrimeFaces problem (same would happen with plain jsf too) and it sort of is not a 'java' problem either but a plain coding issue.
My datatable has a 'Delete' button on each data field.
On clicking, a confirm dialog will be shown with message as Are you sure you want to delete 'dataId'?
I tried to use p:confirm and customize it's message attribute.
Problem is that I can't call 'dataId' from data field.
You can take a look at my code here:
<p:dataTable id="trip-dtbl" rows="10"
value="#{tripBean.tripsList}" var="trip">
<p:column headerText="ID">
<h:outputLabel value="#{trip.id}" />
</p:column>
<p:column headerText="Action" >
<p:commandButton icon="ui-icon-trash" update="trip-dtbl"
action="#{tripsBean.removeTrip(trips)}">
<p:confirm header="Confirm Delete"
message="Are you sure you want to delete #{trip.id}?" />
</p:commandButton>
</p:column>
</p:dataTable>
The 'trip.id' displayed in the table column but not in confirmDialog.
Why that happen?
I have a problem with primefaces datatables. I have one datatable with some entries and a column with a button inside. If the button is pressed a popup is opened with another datatable. The entries in the second datatable are depending on the row in which the button is pressed.
<!-- first datatable -->
<h:form id="list">
<p:dataTable id="list1" var="item" value="#{bean1.itemlist}"
rowKey="#{item.id}" selection="#{bean1.selectedItem}"
selectionMode="single">
<p:column headerText="ID">
<h:outputText value="#{item.id}" />
</p:column>
...
<p:column headerText="Edit Entries">
<p:commandButton value="Edit Entries"
actionListener="#{bean2.updateEntries(item)}" ajax="true"
oncomplete="PF('edit_entries').show()" />
</p:column>
</p:dataTable>
<!-- Second datatable in the popup -->
<p:dialog header="Edit Entries" widgetVar="edit_entries" modal="true"
resizable="false">
<p:dataTable id="list2" var="entry"
value="#{bean2.entriesList}" rowKey="#{entry.id}"
selection="#{bean2.selectedEntry}" selectionMode="single">
<p:column headerText="Entry Number">
<h:outputText value="#{entry.number}" />
</p:column>
</p:dataTable>
<f:facet name="footer">
<p:commandButton value="Save" oncomplete="PF('edit_entries').hide()" />
</f:facet>
</p:dialog>
</form>
Bean2
public void updateEntries(Item selectedItem) {
this.entriesList = this.entriesQuery.getAllEntriesByItemID(selectedItem.getId());//db query could take some time
System.out.println("entrieslist size: " + this.entriesList.size()); //prints the correct size
}
The problem is that there are no entries listed in the popup datatable although there are some in the list after the db query.
Any ideas how to fix this bug?
Thanks in advance!
UPDATE 1:
<!-- first datatable -->
<h:form id="list">
<p:dataTable id="list1" var="item" value="#{bean1.itemlist}"
rowKey="#{item.id}" selection="#{bean1.selectedItem}"
selectionMode="single">
<p:column headerText="ID">
<h:outputText value="#{item.id}" />
</p:column>
...
<p:column headerText="Edit Entries">
<p:commandButton value="Edit Entries" update=":dialogUpdateEntries"
actionListener="#{bean2.updateEntries(item)}" ajax="true"
oncomplete="PF('edit_entries').show()" />
</p:column>
</p:dataTable>
</h:form>
<!-- Second datatable in the popup -->
<p:dialog header="Edit Enries" id="dialogUpdateEntries" widgetVar="edit_entries" modal="true"
resizable="false">
<h:form id="formEntriesList">
<p:dataTable id="list2" var="entry"
value="#{bean2.entriesList}" rowKey="#{entry.id}"
selection="#{bean2.selectedEntry}" selectionMode="single">
<p:column headerText="Entry Number">
<h:outputText value="#{entry.number}" />
</p:column>
</p:dataTable>
<f:facet name="footer">
<p:commandButton value="Save" oncomplete="PF('edit_entries').hide()" />
</f:facet>
</form>
</p:dialog>
You're indeed not updating the data table in the dialog. JSF doesn't automatically update the view on change of the model, you have to explicitly tell the view to do so. You can use the ajax action component's update attribute for this. This takes a JSF client ID which can be found by rules as outlined in this related answer: How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar".
Given the markup as shown in the question, that'll be
<p:commandButton ... update=":list:list2" />
However, there's another potential problem. You're using <p:dialog modal="true"> inside a form instead of giving the dialog its own form. This way the dialog may not be available in the HTML DOM tree as JavaScript (the one responsible for dealing with ajax stuff) would expect to find it. Giving the dialog its own form should fix this matter. It'll also fix the potential future problems with invoking actions from inside the dialog as handled in this related question: <p:commandbutton> action doesn't work inside <p:dialog>.
<h:form id="viewForm">
...
<p:commandButton ... update=":editDialog" />
...
</h:form>
<p:dialog id="editDialog" modal="true">
<h:form id="editForm">
...
</h:form>
</p:dialog>
Try adding update="list2" to Edit Entries command button (even update="#widgetVar(edit_entries)" should work).
If, because of page layout and structure, you can't target the second datatable using above suggestions, then add styleClass="tList2" to second table, and update it with update="#(.tList2)" on edit button.
I have this form with a panel grid and a dialog:
<h:form id="regiForm">
<p:panelGrid>
<p:row style="height:30%">
<p:column>
<h:outputText/>
</p:column>
<p:column>
<p:commandButton style="width:350px" type="submit" actionListener="#
{regiBean.showDialog}" id="ajax" value="#{msg['regi_button']}" update="regiForm" process="#this"/>
</p:column>
<p:column>
</p:column>
</p:row>
</p:panelGrid>
<p:dialog id="dialog" header="#{msg['regi_dialog_header']}" widgetVar="myDialog" position="center center" >
<h:outputText value="#{msg['regi_dialog']}" />
</p:dialog>
</h:form>
I would like to open the dialog from inside the action listener:
public void showDialog() {
RequestContext.getCurrentInstance().execute("dialog.show()");
RequestContext.getCurrentInstance().execute("myDialog.show()");
}
However, the dialog is not shown. How is this caused and how can I solve it?
I'm using JSF 2.1 and PrimeFaces 3.5.
The first statement with the command
RequestContext.getCurrentInstance().execute("dialog.show()");
won't work because dialog refers to the XHTML ID of the p:dialog component. This will cause an javascript error. And that could be the reason why the second command
RequestContext.getCurrentInstance().execute("myDialog.show()");
won't get executed.
Also I would add ; to the end of each Javascript command (but this is just my personal style)
I am trying to update a primefaces form (wizard-first step) after an ajax command button request with no success. The command button that makes the actual ajax call is on a dialog. I would like after submitting the request to force my view to refresh.
The command button deletes a record from a datatable. It works fine, record is deleted but when dialog is hidden the datatable keeps displaying the deleted record. I would like to force it somehow to refresh. Any ideas? Could I do it from my backing bean reviewManagerBean.deleteProtocol() method?
Here is the code (my BackBean is viewScoped):
<h:form id="reviewManagerForm">
...
<pe:masterDetail id="masterDetail" level="#{reviewManagerBean.currentLevel}" showBreadcrumb="false" selectLevelListener="#{reviewManagerBean.levelListener}" >
<f:facet name="header" >
<h:panelGroup layout="block" style="margin-top: 10px;" >
<h:panelGroup styleClass="levelTitle ui-state-default ui-corner-all wizard-breadcrumbs#{reviewManagerBean.currentLevel eq 1 ? 'ui-state-hover' : ''}">
<h:outputText value="1: Protocol picker"/>
</h:panelGroup>
...
<p:messages id="mainMessagesPanel" showDetail="true" closable="true" />
</h:panelGroup>
</f:facet>
<pe:masterDetailLevel level="1">
<p:panel id="panel1" header="List of available protocols">
<p:dataTable id="protocolsDataTable" var="cRProtocol" rowKey="#{cRProtocol.revProtId}" value="#{reviewManagerBean.cRReviewProtocolList}"
widgetVar="protocolsTable"
...
selection="#{reviewManagerBean.selectedReviewProtocol}"
selectionMode="single" >
...
<p:ajax event="rowSelect" listener="#{reviewManagerBean.setSelectedRow}" />
<p:ajax event="rowUnselect" listener="#{reviewManagerBean.unsetSelectedRow}"/>
...
<p:column sortBy="#{cRProtocol.revProtTitle}" headerText="Title" style="width:200px;text-align:center;">
<h:outputText value="#{cRProtocol.revProtTitle}" />
</p:column>
...
<p:column>
<p:commandButton value="Delete" onclick="dlg5.show()"
update=":reviewManagerForm:deleteSingleProtocol"
disabled="#{reviewManagerBean.checkifProtocolIsOpen(cRProtocol)}"
ajax="true" process=":reviewManagerForm:deleteSingleProtocol" />
</p:column>
</p:dataTable>
</p:panel>
...
<p:dialog id="dialog-deleteprotocol" header="Delete Image Type" widgetVar="dlg5" dynamic="true" modal="true" resizable="false">
<p:panelGrid id="deleteSingleProtocol">
<p:row>
<p:column>
<h:outputText value="Id:" style="font-weight:bold; padding-right:10px" />
</p:column>
<p:column>
<h:outputText value="#{reviewManagerBean.selectedReviewProtocol.revProtId}" />
</p:column>
</p:row>
...
<p:column>
<p:commandButton id='protocolDelete'
value='Delete'
action='#{reviewManagerBean.deleteProtocol()}'
ajax="true"
onclick="dlg5.hide()" icon="ui-icon-disk"
update=":reviewManagerForm:protocolsDataTable"
process=":reviewManagerForm:deleteSingleProtocol" />
</p:column>
</p:row>
</p:panelGrid>
</p:dialog>
Your datatable should be re-rendered properly, you would have gotten an exception otherwise (something like component with id :reviewManagerForm:protocolsDataTable could not be found).
This means that #{reviewManagerBean.cRReviewProtocolList} will get called again. You need to remove the item from that list. This is appropriately done from #{reviewManagerBean.deleteProtocol}.
When #{reviewManagerBean.cRReviewProtocolList} is called after the removal, your datatable will get updated.