0

I'm using Primefaces datatable and have a column containing delete button. And I'm facing similar problem like here : Primefaces Delete & Confirm Dialog inside table column - Update or Freeze I tried the solution given there but it's not working. In my case the method vehiTypeListMngr.deletVehicleType registered with actionListener of <p:commandButton is not getting invoked. Also #{vehiTypeListMngr.selectedVehiType.type} is displayed null in the confirm dialog box. Any idea why it's not working?

<h:body>
   <h:form id="formListVehiType">

   <p:dataTable id="vTypeTable" var="vType"
       value="#{vehiTypeListMngr.vehiTypes}"
       rowKey="#{vType.vehicleTypeId}" style="margin-bottom:20px"
       paginator="true" rows="10"
       paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
       currentPageReportTemplate="{startRecord}-{endRecord} of {totalRecords} records"
       rowsPerPageTemplate="5,10,15"
       selection="#{vehiTypeListMngr.selectedVehiType}"
       selectionMode="single">

       <p:column headerText="VehicleTypeId" visible="none">
           <h:outputText value="#{vType.vehicleTypeId}" />
       </p:column>
       <p:column headerText="Vehicle Type" style="width:120px;">
           <h:outputText value="#{vType.type}" />
       </p:column>
       <p:column headerText="Delete" style="width:45px; text-align: center;">
           <p:commandButton value="" icon="ui-icon-delete" type="button" styleClass="ui-confirmdialog-yes" oncomplete="PF('deleteDlg').show();" update=":formListVehiType:display">
           <f:setPropertyActionListener value="#{vType}" target="#{vehiTypeListMngr.selectedVehiType}" />
       </p:commandButton>
       </p:column>
   </p:dataTable>

   <p:dialog id="confirmDeleteDialog" widgetVar="deleteDlg"
       showEffect="fade" hideEffect="explode" header="Confirm" severity="alert" modal="true">

       <h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">
       <h:outputText value="Are you sure you want to delete?" />
           <h:outputText value=" "/>
           <h:outputText value="#{vehiTypeListMngr.selectedVehiType.type}" style="font-weight:bold" />
           <h:outputText value=" "/>
           <p:commandButton id="confirm" value="Yes" type="button" 
           actionListener=
           "#{vehiTypeListMngr.deletVehicleType((vehiTypeListMngr.selectedVehiType))}"
           styleClass="ui-confirmdialog-yes" icon="pi pi-check"
           update=":formListVehiType:vTypeTable" oncomplete="deleteDlg.hide()"/>
           <p:commandButton value="No " type="button" styleClass="ui-confirmdialog-no" 
           icon="pi pi-times" onclick="deleteDlg.hide()" />
       </h:panelGrid>
   </p:dialog>
   </h:form>
</h:body>

The Managed bean :

import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named(value = "vehiTypeListMngr")
@ViewScoped
public class VehicleTypeListManager extends CommonUtilsJSF implements Serializable {

   private static final long serialVersionUID = 4966431989099484036L;
   private static Logger _logger = LogManager.getLogger(VehicleTypeListManager.class);
   @EJB
   private TravelService travelService;
   private String searchString = "";

   private List<VehicleTypeTO> vehiTypes;
   private VehicleTypeTO selectedVehiType;

   public void deletVehicleType() {
       try {
           travelService.deleteVehicleType(selectedVehiType.getVehicleTypeId());
           vehiTypes.remove(selectedVehiType);
           addInfoMessage(String.format("Vehicle Type %s deleted.", selectedVehiType.getType()));
           selectedVehiType = null;
       } catch(CabSysAppException ex) {
           String errMsg = String.format("Error while deleting Vehicle Type : %s -> %s",
           selectedVehiType.getType(), ex.getMessage());
           _logger.debug(errMsg);
           addExceptionMessageError(errMsg);
   }
}
techStack
  • 103
  • 6
  • It seems the problem is with the not triggering the actionListener. There could be different reasons for that. I would suggest you to check why the actionListener is not firing by creating a simple page with a form and a button. Maybe there is some configurational issue related to Ajax of Primefaces. Also check if JavaScript is enabled in your browser. – Tapas Bose Oct 27 '20 at 13:19
  • I tried your example in a separate page and it's working. In my case in confirm dialog I had set type = "button" for Yes and No buttons and so it was not working. Removed type = "button" and now confirm dialog is displaying, but the object `selectedVehicleType` is null. Now the method `deletVehicleType(VehicleTypeTO selectedVehicleType)` is also getting invoked but the parameter `selectedVehicleType` passed to it is null. What could be wrong ? – techStack Oct 27 '20 at 17:56
  • You need to update the dialog from the button of the data table. Also open the dialog on oncomplete not onclick, since oncomplete is fired after the update is done. – Tapas Bose Oct 27 '20 at 18:36
  • @TapasBose the dialog is now displayed with the selectedVehicleType value ` – techStack Oct 28 '20 at 13:22
  • You don't need to pass the value as a parameter to the actionListener. You already have it in your ManagedBean. So when the actionListener of the delete button of that dialog will be triggered, the corresponding method will have it. Also please provide an update of your code by editing your question. – Tapas Bose Oct 28 '20 at 13:38
  • Already edited the code - now I have changed the deletVehicleType() method signature by removing the parameter. I have tried before both ways by passing the parameter and without parameter. In the popup dialog code you can see that `` the value - Vehicle Type - is being displayed in the dialog after hitting the delete button in datatable. But in the Managed bean method deletVehicleType() the `selectedVehiType` is `null` and throwing `NullPointerException`. What's going wrong? – techStack Oct 28 '20 at 16:29
  • Hi, I have asked you to open the dialog with oncomplete, not with onclick. Have you tried that? Your updated code is not showing it! – Tapas Bose Oct 28 '20 at 16:31
  • Sorry, forgot to update that part, yes I have changed it to oncomplete="PF('deleteDlg').show();", forgot to update here in the question. Updated. – techStack Oct 28 '20 at 16:39
  • @TapasBose I change `javax.faces.view.ViewScoped` to Omnifaces' `org.omnifaces.cdi.ViewScoped` and a very strange behaviour it showed. First time I tried to delete the VehicleType with Omnifaces' `@ViewScoped` annotation. It worked without any exception - the VehicleType got deleted from the DB as well the row got deleted from the datatable. But it worked with success only once! Later when I tried again with `javax.faces.view.ViewScoped` the same `NullPointerException` was thrown and now it's not even working with Omnifaces' `@ViewScoped`. – techStack Oct 29 '20 at 05:14
  • Now I wonder how exactly `` works ? OR is it a bug with using the combination of CDI `@ViewScoped` and `javax.inject.Named` ? – techStack Oct 29 '20 at 05:15
  • Read this SO answer: https://stackoverflow.com/a/14384824/576758, it will help. – Tapas Bose Oct 29 '20 at 07:15
  • The default value of the process attribute is @all. See this: https://primefaces.github.io/primefaces/8_0/#/components/commandbutton. If your form has had any required field then the actionListener won't have fired and I don't know if have taken any way to show the required validation failed messages. Thanks for reading the Primefaces showcase and the documentation for the growl first. – Tapas Bose Oct 29 '20 at 12:05
  • Hi, Solved the issue by adding process="@this" to the Confirm commandButton. Thanks for the help! Found this SO answer useful https://stackoverflow.com/a/25347186/11577562 -> Excerpt: Note that it's in case of ActionSource components (such as ) very important that you also include the component itself in the process attribute, particularly if you intend to invoke the action associated with the component. – techStack Oct 29 '20 at 12:20

0 Answers0