2017-07-08 20 views
5

Tôi muốn tạo biểu mẫu chỉnh sửa cho bảng Primefaces. Tôi đã thử nghiệm mã này: đậuTạo biểu mẫu chỉnh sửa cho bảng Primefaces

<h:form id="form"> 
    <p:dataTable id="tbl" var="systemuser" value="#{systemusers.systemUsers}"         
       selectionMode="single" selection="#{systemusers.selectedSystemUser}" rowKey="#{systemuser.username}" lazy="true" 
       resizableColumns="true" 
       > 

     <p:ajax event="rowSelect" listener="#{systemusers.onRowSelect}" update=":form:carDetail" oncomplete="PF('carDialog').show()" /> 
     .................... 
    </p:dataTable> 

    <p:dialog id="wd" header="System User Details" widgetVar="carDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="true"> 
     <p:outputPanel id="carDetail" style="text-align:center;"> 
      <p:panelGrid columns="2" columnClasses="label,value"> 

       <h:outputLabel for="id" value="ID" /> 
       <p:outputLabel id="id" value="#{systemusers.selectedSystemUser.id}" rendered="#{not systemusers.editable}"/> 
       <h:inputText value="#{systemusers.selectedSystemUser.id}" rendered="#{systemusers.editable}" /> 
       ........ 
      </p:panelGrid> 
     </p:outputPanel> 

     <f:facet name="footer"> 
      <p:commandButton value="Edit System User" rendered="#{not systemusers.editable}" 
          actionListener="#{systemusers.editRecord(false)}"> 
       <f:ajax render=":form:wd" execute=":form:wd"></f:ajax> 
      </p:commandButton>&nbsp; 

      <p:commandButton value="Cancel" rendered="#{systemusers.editable}" actionListener="#{systemusers.editRecord(true)}"> 
       <f:ajax render=":form" execute=":form"></f:ajax> 
      </p:commandButton> 
     </f:facet> 
    </p:dialog> 

    <p:contextMenu for="tbl"> 
     <p:menuitem value="View" update="carDetail" icon="fa fa-search" oncomplete="PF('carDialog').show()"/> 
     <p:menuitem value="Delete" update="tbl" icon="fa fa-remove" actionListener="#{systemusers.deleteSystemUser}"> 
      <p:confirm header="Confirmation" message="Are you sure?" icon="fa fa-warning" /> 
     </p:menuitem> 
    </p:contextMenu> 

    <p:confirmDialog global="true" showEffect="fade" hideEffect="fade"> 
     <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="fa fa-check" /> 
     <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="fa fa-remove" /> 
    </p:confirmDialog> 
</h:form> 

Managed:

@Named 
@RequestScoped 
public class Systemusers implements Serializable 
{ 
....... 
public void editRecord(boolean editable) 
    { 
     SessionFactory factory = HibernateUtils.getSessionFactory(); 
     Session session = factory.getCurrentSession(); 

     try 
     { 
      session.getTransaction().begin(); 

      SystemUsersModel obj = new SystemUsersModel(); 
      obj.setId(selectedSystemUser.getId()); 
      .......... 
      session.update(obj); 

      session.getTransaction().commit(); 

      this.editable = editable; 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      session.getTransaction().rollback(); 
     } 
    } 
...... 
private boolean editable = false; 

    public boolean isEditable() 
    { 
     return editable; 
    } 

    public void setEditable(boolean editable) 
    { 
     this.editable = editable; 
    } 
} 

Khi tôi mở biểu mẫu Chỉnh sửa và tôi bấm nút Edit System User hình thức biến mất. Có lẽ vì tôi render và thực hiện nó. Làm thế nào tôi có thể thực hiện các hình thức và dưới nó mà không đóng nó?

+0

Tôi đã thử nhưng tôi nhận được kết quả tương tự. Bạn có thể dán một số ví dụ mã? –

+0

Tôi đã thử đề xuất của bạn nhưng một lần nữa hộp thoại bị ẩn. Tôi đã thêm biểu mẫu vào hộp thoại và giờ đây tôi đã giải quyết được vấn đề. Hiện tại tôi đang đối mặt với vấn đề này: Java Object value = "# {systemusers.selectedSystemUser.id}" trống rồi tôi nhấn nút chỉnh sửa. Bất kỳ ý tưởng làm thế nào tôi có thể sửa lỗi này? –

+0

Một lưu ý nhanh ở đây là bạn tốt hơn có một h: hình thức dành riêng cho hộp thoại và một cho bảng, cố gắng để bọc ** nội dung ** của p: hộp thoại với một h: hình thức mới và để dataTable của bạn với cũ h: hình thức, điều này có thể dẫn bạn sửa bất kỳ vấn đề xử lý hoặc xử lý sai nào. –

Trả lời

1

Mã ví dụ không hợp lệ theo nhiều cách. Hai điều nổi bật:

  • Nó không phải là một [mcve]
  • <f:ajax render=":form" execute=":form"> là không cần thiết hoặc thậm chí có thể gây ra những hành vi kỳ lạ

OP rất có thể không biết rằng (trích dẫn từ các PrimeFaces showcase, tôi nhấn mạnh)

CommandButton

CommandButton mở rộng tiêu chuẩn h: commandButton với ajax, xử lý một phần và tính năng skinning.

Vì vậy, các commandBotton đã ajax anabled và làm không cần để HAVA một

<f:ajax render=":form" execute=":form"> 

bên trong nó. Điều này (rất có thể) làm cho bản cập nhật chạy hai lần. Một @none (Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes) và một cập nhật biểu mẫu. Cách thứ hai rất có thể không có bất kỳ nội dung nào. Nhưng cố gắng (và gỡ lỗi) sẽ làm mọi thứ rõ ràng. Nếu đây không phải là câu trả lời, lý do không thể thấy trong mã và do đó nó phải là [mcve]

Các vấn đề liên quan