如何在对话框关闭后保持选定的行Primefaces

时间:2015-08-22 06:51:38

标签: primefaces

在Primefaces 5.2,Glassfish 4.0中,我正在开发一个简单的CRUD。我有两个与行选择有关的不同问题。

用户点击数据表中的一行,此行突出显示,用户选择"编辑"在上下文菜单中的菜单中,将打开一个模态窗口。

1 /当用户按"取消"在模态窗口中的按钮,对话框正确关闭。但是,如果用户选择数据表中的另一行,则新模态窗口中显示的值对应于第一个先前选择。如何更新选定的值?

2 /当用户按下" OK"在模态窗口中的按钮,对话框正确关闭,数据表正确更新。但是数据表中更新的行不再突出显示,用户不知道哪一行刚刚更新。有没有办法保持突出显示最后更新的行(但重新初始化选定的值以进一步选择),直到下一次单击一行,并与其他焦点行并行?

searchCompany.xhtml代码是:

            <h:form id="form">
                 <p:growl id="msgs" showDetail="true"/>

                 <p:contextMenu for="table">
                    <p:menuitem value="View" update="detail" icon="ui-icon-search" oncomplete="PF('companyDialog').show()"/>
                    <p:menuitem value="Delete" update="table" icon="ui-icon-close" actionListener="#{dtContextMenuView.deleteCompany}"/>
                </p:contextMenu>

                <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"> 
                        <!-- above: by default table is sorted by Company name -->
                        <!-- above: to freeze the 2 first columns : frozenColumns="2"-->

                    <p:column
                        style="width:150px"
                        sortBy="#{company.name}"
                        filterBy="#{company.name}"
                        filterMatchMode="contains"> 
                        <!-- Column is sortable --> 
                        <f:facet name="header">
                            #{msg['name']}  <!-- Title of the column --> 
                        </f:facet>                  
                        <p:cellEditor>
                            <f:facet name="output">
                            <h:outputText value="#{company.name}" />
                        </f:facet>
                        <f:facet name="input">
                            <h:inputText value="#{company.name}"/>
                            </f:facet>
                        </p:cellEditor>
                    </p:column>
(..)
                </p:dataTable>

                <p:dialog header="#{msg['detail']}" widgetVar="companyDialog" modal="true" showEffect="fade" resizable="false">
                    <p:panel id="detail" >
                        <p:panelGrid columns="2" rendered="#{not empty companyBean.selectedCompany}">
                            <h:outputText value="#{msg['name']}:" />
                            <p:inputText value="#{companyBean.selectedCompany.name}" />
                            <h:outputText value="#{msg['contact']}:" />
                            <p:inputText value="#{companyBean.selectedCompany.contact}" />
                        </p:panelGrid>
                        <p:commandButton action="#{companyBean.updateCompanyEnd}" value='#{msg.OK}' oncomplete="PF('companyDialog').hide();" update="table, :form:msgs"/>
                        <p:commandButton value='#{msg.cancel}' onclick="PF('companyDialog').hide();"/>
                    </p:panel>
                </p:dialog>

        </h:form>
    </ui:define>
</ui:composition>

bean代码是:

package com.sdzee.bean;

@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<Company> 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<Company> 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;
 }

}

0 个答案:

没有答案