jsfprimefacesprimefaces-datatable

Primefaces - Customize p:confirmDialog content conditionally


I have a list of objects (let's call each object a record object), shown through a <p:datatable> component. Each record has a delete button column.

When a user clicks on the delete button of the record, a <p:confirmDialog> is shown, asking for user confirmation. What I want is to customize the content of the confirmDialog under corresponding circumstances (for example show/do not show a checkbox concerning the value of a property of the record, f.e if record.isPersonal, show the checkbox, else not.

Unfortunately, that does not seem to be working as the checkbox is always shown in case the first record satisfies the condition, and the opposite in case it does not. After some research I found out that especially in previous Primefaces versions, they used to use the "JS way" (creating two separate confirm dialogs and proportionally use PF('widgeName').show(), but I would like to know whether any out of the box solution exists in Primefaces 7.0 version which I use through the <p:confirm> tag or something else. Code example below:

<p:dataTable id="recordsTable" lazy="true" value="#{myBean.myList.records}" var="record...">

          <p:column styleClass="deleteColumn">
    
                        <p:commandButton ...>
                 
                            <p:confirm .../>
    
                        </p:commandButton>
           
          <p:confirmDialog widgetVar="delete_record_dialog" global="true" showEffect="fade" hideEffect="fade">
    
                            <p:selectBooleanCheckbox
                                    rendered="#{record.isPersonal}"
            ....>
           </p:selectBooleanCheckbox>
    
    
            <p:commandButton value="#{msg.yes}" type="button"
                             styleClass="ui-confirmdialog-yes" icon="pi pi-check"
            />
            <p:commandButton value="#{msg.no}" type="button" styleClass="ui-confirmdialog-no"
                             icon="pi pi-times"
            />
    
    </p:confirmDialog>

  </p:column>

</p:dataTable>

Thanks in advance!


Solution

  • I think rather than using ConfirmDialog, you may have to revert to building you own custom dialog, either using p:dialog or using the dialog framework. Using dialog framework you can create a simple confirm dialog box and can pass the data into the dialog programatically - e.g. a flag based on the current row to indicate if the checkbox should be shown. The dialog framework also gives an easy way to return data back from the dialog to the calling page using the dialogReturn ajax event.