Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying Primefaces confirmDialog from Backing Bean

I have a Primefaces datatable and when the user clicks on a row, I display the data to edit in a form. If the user changes the data in the form and clicks on any other row i.e if there is dirty data, I need to popup a confirmDialog to show if the user wants to save the data / discard it. The confirmDialog does not display when I try to execute it from backing bean. Any help is appreciated!

I have implemented it as follows:

.xhtml:

<p:dataTable id="tsTableId" value="#{transactionSetBean.studentList}" var="tsRow"
     selectionMode="single" selection="#{transactionSetBean.selectedEditRec}" rowKey="#{tsRow.id}" scrollRows="10">
    <p:ajax event="rowSelect" listener="#{transactionSetBean.onRowSelect}" update=":transactionSetsForm:tsEntryFrmId">
    </p:ajax>
..
</p:dataTable>

ConfirmDialog:

<p:confirmDialog widgetVar="dataChangeDlg"  message="Save changes Or Cancel">                                
<p:commandButton value="Save Changes" oncomplete="PF('dataChangeDlg').hide();" 
          update=":transactionSetsForm:messages :transactionSetsForm:tsEntryFrmId" 
              action="#{transactionSetBean.updateRecord}" />
<p:commandButton value="Cancel"   onclick="PF('dataChangeDlg').hide();"                             

</p:confirmDialog>

Backing Bean:

public void onRowSelect(SelectEvent event)
    {
        String actionName = ON_ROW_SELECT;
        try
        {
            Student selectedObj = (Student)event.getObject();
            if (selectedObj != null)
            {
                selectedEditRec = selectedObj;
            }
            // if data is changed then show the dataChange dialog 
            if (isDataChanged())
            {
                setShowDataChangedDialog(true);
                RequestContext context = RequestContext.getCurrentInstance();
                // execute javascript and show dialog
                context.execute("PF('dataChangeDlg').show();");
            }

        }
        catch (Exception e)
        {
            handleException(e);
        }
    }
like image 251
srmthy4 Avatar asked Jun 19 '14 13:06

srmthy4


2 Answers

RequestContext.getCurrentInstance().execute("PF('dataChangeDlg').show();");

<p:ajax event="rowSelect" listener="#{transactionSetBean.onRowSelect}" update=":transactionSetsForm:tsEntryFrmId">

works for me. There must be another error. maybe isDataChanged is false, wrong component ids in update or something.

like image 95
user3424032 Avatar answered Nov 12 '22 13:11

user3424032


With PrimeFaces >= 6.2

PrimeFaces.current().executeScript("PF('dataChangeDlg').show()");
like image 30
menredo Avatar answered Nov 12 '22 15:11

menredo