Search code examples
primefacesdatatable.refresh

Primefaces Delete & Confirm Dialog inside table column - Update or Freeze Issue


I have a smiliar problem like here: Primefaces: commandButton in confirmDialog cannot update datatable in the same form

I have a table with games. In the last column there are 2 buttons: delete and details.

The delete button should delete the game

The button does delete the game but the update doesn't work.

  • update=":form1:overviewTableGame" --> no reaction (no refresh)

  • update="@form" --> update performes (table refreshes), but the entire scren is locked. i think due to the fact, that the form which contains the dialog is updated...

The table code:

<h:form id="form1">
        <p:messages id="messages" showDetail="true" autoUpdate="true"
            closable="true" />

        <p:dataTable id="overviewTableGame" var="game" value="#{gameMB.list}">
            <p:column headerText="#{msg.ID}" sortBy="#{game.id}">
                <h:outputText value="#{game.id}" />
            </p:column>

            <p:column headerText="#{msg.NAME}" sortBy="#{game.name}">
                <h:outputText value="#{game.name}" />
            </p:column>

            <p:column headerText="#{msg.DESCRIPTION}"
                sortBy="#{game.description}">
                <h:outputText value="#{game.description}" />
            </p:column>

            <p:column headerText="#{msg.ADMIN}" sortBy="#{game.admin.firstname}">
                <h:outputText value="#{game.admin.firstname}" />
            </p:column>

            <p:column headerText="#{msg.ACTION}">
                <p:commandButton id="commandButtonDELETE" value="löschen"
                    onclick="confirmation.show()" type="button"
                    update=":form1:display">
                    <f:setPropertyActionListener value="#{game}"
                        target="#{gameMB.selectedGame}" />
                </p:commandButton>

                <p:commandButton id="commandButtonDETAIL" value="detail"
                    action="#{gameMB.details()}">
                    <f:param name="id" value="#{game.id}" />
                </p:commandButton>
            </p:column>
        </p:dataTable>

        <p:confirmDialog id="confirmDialog"
            message="Are you sure about destroying the world?"
            header="Initiating destroy process" severity="alert"
            widgetVar="confirmation">


            <h:panelGrid id="display" columns="2" cellpadding="4"
                style="margin:0 auto;">

                <h:outputText value="Name:" />
                <h:outputText value="#{gameMB.selectedGame.name}"
                    style="font-weight:bold" />

                <p:commandButton id="confirm" value="Yes Sure"
                    oncomplete="confirmation.hide()"
                    actionListener="#{gameMB.delete(gameMB.selectedGame)}"
                    update=":form1:overviewTableGame">
                </p:commandButton>
            </h:panelGrid>

            <p:commandButton id="decline" value="Not Yet"
                onclick="confirmation.hide()" type="button" />
        </p:confirmDialog>

    </h:form>

The Delete Method:

public void delete(Game game) {
    //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"DELETE", "Game Deleted!"));
    System.out.println("==================");
    System.out.println(game);
    getGameService().removeGame(game);
    //this.gameList = gameService.listAllGames();
}

The selectedGame

private Game selectedGame;

public Game getSelectedGame() {
    return selectedGame;
}

public void setSelectedGame(Game selectedGame) {
    this.selectedGame = selectedGame;
}

Any ideas?

Thanks


Solution

  • Separate your dialog from p:dataTable. Following code is working:

    The xhtml:

    <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" template="/WEB-INF/templates/globalTemplate.xhtml">
    
        <ui:define name="title">15344819</ui:define>
        <ui:define name="content">
            <p:growl id="growl" showDetail="true" />
    
            <h:form id="form">
                <p:dataTable id="students" value="#{so15344819.students}" var="student">
                    <p:column>
                            <p:commandButton id="selectButton" update=":form:display" oncomplete="studentDialog.show()" icon="ui-icon-search" title="View">
                               <f:setPropertyActionListener value="#{student}" target="#{so15344819.selectedStudent}" />
                           </p:commandButton>
                       </p:column>
                </p:dataTable>
    
                <p:dialog header="Student Detail" widgetVar="studentDialog" resizable="false" id="studentDlg"
                                showEffect="fade" hideEffect="explode" modal="true">
    
                        <h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">
    
                            <h:outputText value="Name:" />
                            <h:outputText value="#{so15344819.selectedStudent.name}" style="font-weight:bold"/>                                               
                            <p:commandButton id="deleteButton" actionListener="#{so15344819.delete(so15344819.selectedStudent)}" oncomplete="studentDialog.hide()" 
                                update=":form:students" value="Delete"/>
                            <p:commandButton id="cancelButton" onclick="studentDialog.hide()" value="Cancel"/>
                        </h:panelGrid>
    
                    </p:dialog>
            </h:form>
        </ui:define>
    
    </ui:composition>
    

    The managed bean:

    package app.so.dev.web.controller;
    
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;
    
    import app.so.dev.web.model.Student;
    
    @ManagedBean(name = "so15344819")
    @ViewScoped
    public class SO15344819 implements Serializable {
    
        private static final long serialVersionUID = 6686378446131077581L;
        private List<Student> students;
        private Student selectedStudent;
    
        @PostConstruct
        public void init() {
            students = new ArrayList<Student>();
            students.add(new Student("Student 1"));
            students.add(new Student("Student 2"));
        }
    
        public void delete(Student student) {
            System.out.println("==================");
            System.out.println(student);
            students.remove(student);
        }
    
        public List<Student> getStudents() {
            return students;
        }
    
        public void setStudents(List<Student> students) {
            this.students = students;
        }
    
        public Student getSelectedStudent() {
            return selectedStudent;
        }
    
        public void setSelectedStudent(Student selectedStudent) {
            this.selectedStudent = selectedStudent;
        }
    }
    

    Feel free to revert.