繁体   English   中英

在jsf中更新数据表中的选定行

[英]Updating a selected row in datatable in jsf

我正在创建一个jsf应用程序,我需要执行CRUD。 到目前为止,我已经设法删除,创建和读取,但是无法更新记录。所以我的问题是,我希望当用户单击“更新”按钮时弹出一个对话框,弹出所选行的详细信息并更新详细信息。 。 这是我的示例代码。

        <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";
        }

改变你的

<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属性用于导航(例如,重定向到另一个页面)。 另外,最好使用oncomplete因为它会在您的ajax请求完成时执行,而不是在您按下按钮时跳过触发验证(跳过您的情况下的对话框)的onclick,从而跳过验证等操作。

如果您的问题出在哪里,那么如果您喜欢第二个代码段,则使用更新/进程(ajax)机制刷新对话框内容时,将使用当前选择内容进行更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM