CommandButton in dataTable

时间:2015-05-04 19:41:53

标签: jsf primefaces commandbutton

I have a datatable to display some informations about users and the last column contains a command button to delete the row. the problem is when i click on any button all rows are deleted that is the action is invoked many times. My Xhtml file:

<h:form>
    <p:dataTable var="user" value="#{admin.users}">
     <p:column headerText="Login">
         <h:outputText value="#{user.login}" />
     </p:column>

    <p:column headerText="Nom">
        <h:outputText value="#{user.nom}" />
    </p:column>

    <p:column headerText="Prenom">
        <h:outputText value="#{user.prenom}" />
    </p:column>

    <p:column headerText="Password">
       <h:outputText value="#{user.password}" />
    </p:column>
   <p:column headerText="Operation">
     <p:commandButton value="supprimer" onClick="#{admin.delete_user(user.login)}" ajax="false" />
   </p:column>
 </p:dataTable>
</h:form>

My bean

  @ManagedBean (name="admin")
  @ViewScoped
  public class Admin {
  private ArrayList<User> users;
  //getters & setters
  public String delete_user(String login)
{

     System.out.println(login);

    UserDao ud=new UserDao();

    try{


      ud.Delete_Query(login);

    }
    catch(Exception e ){System.out.println(e.getStackTrace());}




  return"#";   
}

}

when i click the commandbutton in any row , all users login are printed in the console.

1 个答案:

答案 0 :(得分:0)

您使用EL在onclick中的backbean中调用方法,您应该将其放在action中。 Onclick仅适用于javascript。

但是这个stil并没有解释你描述的行为(但可能是他们的日志行已经打印出来了,而你只是在想到&#39;当你点击按钮时它们会打印出来。)我&#39; d期望在呈现页面时发生所有删除操作。