title: How to keep selected row after dialog closes Primefaces
body: <p>In Primefaces 5.2, Glassfish 4.0, I am developping a simple CRUD. I have 2 distinct issues related to row selection.</p>
  
  <p>The user clicks on a row in the datatable, this row is highlighted, the user selects "Edit" menu in the contextual menu, a modal window is opened.</p>
  
  <p>1/ When the user press "Cancel" button in the modal window, the dialog is correctly closed. But then if the user selects another row in the datatable, the values displayed in the new modal window corresponds to the first previous selection. How to update the selected values? </p>
  
  <p>2/ When the user press "OK" button in the modal window, the dialog is correctly closed, the datatable is correctly updated. But the updated row in the datatable is not anymore highlighted, the user doesn't know which row has just been updated. Is there a way to keep highlighted the lastly updated row (but to reinitialize the selected values for futher selection) until next click on a row, and in parallel of other focused row?</p>
  
  <p>searchCompany.xhtml code is:</p>
  
  <pre><code>            &lt;h:form id="form"&gt;
                   &lt;p:growl id="msgs" showDetail="true"/&gt;
  
                   &lt;p:contextMenu for="table"&gt;
                      &lt;p:menuitem value="View" update="detail" icon="ui-icon-search" oncomplete="PF('companyDialog').show()"/&gt;
                      &lt;p:menuitem value="Delete" update="table" icon="ui-icon-close" actionListener="#{dtContextMenuView.deleteCompany}"/&gt;
                  &lt;/p:contextMenu&gt;
  
                  &lt;p:dataTable 
                          id="table" 
                          value="#{companyBean.allCompanies}" 
                          var="company"
                          editable="true"
                          editmode="row"
                          widgetVar="cellCompanies"
                          sortBy="#{company.name}"
                          scrollable="true"
                          scrollWidth="1024" 
                          scrollHeight="300"
                          rowKey="#{company.id}"
                          selection="#{companyBean.selectedCompany}" 
                          selectionMode="single"&gt; 
                          &lt;!-- above: by default table is sorted by Company name --&gt;
                          &lt;!-- above: to freeze the 2 first columns : frozenColumns="2"--&gt;
  
                      &lt;p:column
                          style="width:150px"
                          sortBy="#{company.name}"
                          filterBy="#{company.name}"
                          filterMatchMode="contains"&gt; 
                          &lt;!-- Column is sortable --&gt; 
                          &lt;f:facet name="header"&gt;
                              #{msg['name']}  &lt;!-- Title of the column --&gt; 
                          &lt;/f:facet&gt;                  
                          &lt;p:cellEditor&gt;
                              &lt;f:facet name="output"&gt;
                              &lt;h:outputText value="#{company.name}" /&gt;
                          &lt;/f:facet&gt;
                          &lt;f:facet name="input"&gt;
                              &lt;h:inputText value="#{company.name}"/&gt;
                              &lt;/f:facet&gt;
                          &lt;/p:cellEditor&gt;
                      &lt;/p:column&gt;
  (..)
                  &lt;/p:dataTable&gt;
  
                  &lt;p:dialog header="#{msg['detail']}" widgetVar="companyDialog" modal="true" showEffect="fade" resizable="false"&gt;
                      &lt;p:panel id="detail" &gt;
                          &lt;p:panelGrid columns="2" rendered="#{not empty companyBean.selectedCompany}"&gt;
                              &lt;h:outputText value="#{msg['name']}:" /&gt;
                              &lt;p:inputText value="#{companyBean.selectedCompany.name}" /&gt;
                              &lt;h:outputText value="#{msg['contact']}:" /&gt;
                              &lt;p:inputText value="#{companyBean.selectedCompany.contact}" /&gt;
                          &lt;/p:panelGrid&gt;
                          &lt;p:commandButton action="#{companyBean.updateCompanyEnd}" value='#{msg.OK}' oncomplete="PF('companyDialog').hide();" update="table, :form:msgs"/&gt;
                          &lt;p:commandButton value='#{msg.cancel}' onclick="PF('companyDialog').hide();"/&gt;
                      &lt;/p:panel&gt;
                  &lt;/p:dialog&gt;
  
          &lt;/h:form&gt;
      &lt;/ui:define&gt;
  &lt;/ui:composition&gt;
  </code></pre>
  
  <p>The bean code is :</p>
  
  <p>package com.sdzee.bean;</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  
  public class CompanyBean implements Serializable {
  
   private static final long serialVersionUID = 1L;
  
   private static final String CREATE_COMPANY = "createCompany";
   private static final String DELETE_COMPANY = "deleteCompany";
   private static final String UPDATE_COMPANY = "updateCompany";
   private static final String LIST_ALL_COMPANIES = "searchCompany";
   private static final String STAY_IN_THE_SAME_PAGE = null;
   @EJB
   private CompanyFacade companyFacade;
  
   private Company company;
   private List&lt;Company&gt; AllCompanies;
  
   @PostConstruct
   public void init() {
       AllCompanies = companyFacade.findAll();
   }
  
   private Company selectedCompany;
  
   public Company getCompany() {
  
    if(company == null){
     company = new Company();
    }
  
    return company;
   }
  
   public void setCompany(Company company) {
    this.company = company;
   }
  
   public List&lt;Company&gt; getAllCompanies() {
    return AllCompanies;
   }
  
   public String updateCompanyEnd(){
        try {
         Company comp = selectedCompany;
         companyFacade.update(comp);
        } catch (EJBException e) {
         FacesContext context = FacesContext.getCurrentInstance();
         String texte = context.getApplication().evaluateExpressionGet(context, "#{msg['error']}", String.class);
         sendErrorMessageToUser(texte+e);
         return STAY_IN_THE_SAME_PAGE;
        }
        FacesContext context = FacesContext.getCurrentInstance();
        String texte = context.getApplication().evaluateExpressionGet(context, "#{msg['update.successful']}", String.class);
        FacesMessage message = new FacesMessage(texte, selectedCompany.getName());
        FacesContext.getCurrentInstance().addMessage(null, message);
        this.selectedCompany = company;
        return LIST_ALL_COMPANIES;
       }
  
   public String deleteCompanyStart(){
    return DELETE_COMPANY;
   }
  
   public String deleteCompanyEnd(){
    try {
     companyFacade.delete(company);
    } catch (EJBException e) {
     sendErrorMessageToUser("Error. Call the ADM");
     return STAY_IN_THE_SAME_PAGE;
    }   
    FacesContext context = FacesContext.getCurrentInstance();
    String texte = context.getApplication().evaluateExpressionGet(context, "#{msg['delete.successful']}", String.class);
  
    FacesMessage message = new FacesMessage(texte, company.getName());
    FacesContext.getCurrentInstance().addMessage(null, message);
  
    //sendInfoMessageToUser("Operation Complete: Delete");
  
    return LIST_ALL_COMPANIES;
   }
  
   public String createCompanyStart(){
    return CREATE_COMPANY;
   }
  
   public String createCompanyEnd(){
    try {
     companyFacade.save(company);
    } catch (EJBException e) {
     sendErrorMessageToUser("Error. Check if the weight is above 0 or call the adm");
  
     return STAY_IN_THE_SAME_PAGE;
    }  
  
    sendInfoMessageToUser("Operation Complete: Create");
  
    return LIST_ALL_COMPANIES;
   }
  
   public String listAllCompanies(){
    return LIST_ALL_COMPANIES;
   }
  
   private void sendInfoMessageToUser(String message){
    FacesContext context = getContext();
    context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
   }
  
   private void sendErrorMessageToUser(String message){
    FacesContext context = getContext();
    context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));
   }
  
   private FacesContext getContext() {
    FacesContext context = FacesContext.getCurrentInstance();
    return context;
   }
  
   public Company getSelectedCompany() {
       return selectedCompany;
   }
  
   public void setSelectedCompany(Company selectedCompany) {
       this.selectedCompany = selectedCompany;
   }
  
  }
  </code></pre>