2014-11-17 41 views
1

我正在創建一個jsf應用程序,我需要執行CRUD。到目前爲止,我已經設法刪除,創建和讀取,但無法更新記錄。所以我的問題是,我希望當用戶單擊更新按鈕時彈出一個對話框,以選中行的詳細信息並更新詳細信息。這是我的示例代碼。在jsf中更新數據表中選定的行

 <p:panelGrid columns="2"> 
     <h:outputLabel value="Account Id"/> 
     <h:inputText value="#{accCtr.acc.accountNum}" /> 
     <h:outputLabel value="Account Bal"/> 
     <h:inputText value="#{accCtr.acc.balance}"/> 
     <h:outputLabel /> 
     <p:commandButton action="#{accCtr.create()}" value="Enter" update="dt"/> 
    </p:panelGrid> 
      <p:dataTable value="#{accCtr.list}" var="i" id="dt" style="width: 40%;" rowStyleClass="height" rowKey="#{accCtr.acc.accountNum}" > 

       <p:column> 
        <f:facet name="header">Account Num</f:facet> 
        #{i.accountNum} 
       </p:column> 
        <p:column> 
        <f:facet name="header">Account Balance</f:facet> 
        #{i.balance} 
       </p:column> 
        <p:column> 
        <f:facet name="header">Action</f:facet> 
        <p:commandButton value="Remove" styleClass="height" 
            action="#{accCtr.removeAccount(i)}" 
            /> 
        <p:commandButton value="Edit" styleClass="height" 
            onclick="pop.show()" 
            action="#{accCtr.edit(i)}" 
             > 

        </p:commandButton> 
       </p:column> 
      </p:dataTable> 
     </h:form> 
    <p:dialog widgetVar="pop" header="Account Edit"> 
     <h:form> 
      <p:panelGrid columns="2"> 

       <h:outputLabel value="Account Balance"/> 
       <h:inputText value="#{accCtr.acc.balance}"/> 
       <h:outputLabel/> 
       <p:commandButton value="Update"/> 
      </p:panelGrid> 
     </h:form> 
    </p:dialog> 

有人可以幫助我。 和我的支持豆。

@ManagedBean(name="accCtr") 
@SessionScoped 
public class AccountController { 



     List<AccountTable> list=new ArrayList<>(); 
     public AccountController() { 
     } 
     private Account_dao getDao() 
     { 
      return new Account_dao(); 
     } 
     public List<AccountTable> getList() { 

      return getDao().findAll(); 
     } 

     public void setList(List<AccountTable> list) { 
      this.list = list; 
     } 
     public void removeAccount(AccountTable acc) { 
      getDao().remove(acc); 
     } 
     public AccountTable acc=new AccountTable(); 

     public AccountTable getAcc() { 
      return acc; 
     } 

     public void setAcc(AccountTable acc) { 
      this.acc = acc; 
     } 

     public void edit(AccountTable acc) { 
      setAcc(acc); 
     } 
     public String create() 
     { 
      this.acc.setUserid(10); 
      getDao().create(this.acc); 
      return "index"; 
     } 
+0

你的創建方法看起來錯了,我的意思是它的第一行。而你的編輯方法是不完整的。在datatable中有一個名爲selection和selectionMode的屬性,您可以使用該屬性將其綁定到您的支持bean中的選定對象.. http://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml –

+0

@ Sujan創建方法正常工作。我可以插入記錄 – MorganM

+0

您正在對用戶標識進行硬編碼,您是否在進行測試? –

回答

0

更改

<p:commandButton value="Edit" styleClass="height" 
           onclick="pop.show()" 
           action="#{accCtr.edit(i)}" 
            /> 

<p:commandButton value="Edit" styleClass="height" 
           oncomplete="pop.show()" 
           actionListener="#{accCtr.edit(i)}" 
           process="@this" update=":your_dialog_form_id" 
            /> 

有幾件事情:在一般的action atributte用於導航(重定向到例如另一頁)。最好還是使用oncomplete,因爲當你的ajax請求完成時,它會被執行,而不是在你按下按鈕,跳過驗證和這些事情時觸發動作(在你的案例中打開對話框)的onclick。

如果您的問題是,使用更新/進程(ajax)機制刷新對話框內容,將更新您的當前選擇,如果您喜歡第二個片段。