2015-07-02 30 views
0

我在h:dataTable的每行中有一個按鈕,用於刪除記錄。該操作在刪除記錄時起作用。但表格不更新,我不確定有什麼問題?這裏的JSFh:commandButton動作的更新窗體

<h:form id="usersForm" rendered="#{not empty userController.users}"> 
     <h:dataTable id="usersTable" value="#{userController.users}" var="user"> 
      <h:column>#{user.name}</h:column> 
      <h:column>#{user.location.location}</h:column> 
      <h:column> 
       <h:commandButton value="delete" action="#{userController.delete(user)}"> 
        <f:ajax render="usersForm usersTable"/> 
       </h:commandButton> 
      </h:column> 
     </h:dataTable> 
    </h:form> 

編輯:我不相信答案指向是完全一樣的,看看我發現生成的html已經表定義爲<table id="usersForm:usersTable">答案後,所以我修改了JSF如下

<h:form id="usersForm" rendered="#{not empty userController.users}"> 
     <h:dataTable id="usersTable" value="#{userController.users}" var="user"> 
      <h:column>#{user.name}</h:column> 
      <h:column>#{user.location.location}</h:column> 
      <h:column> 
       <h:commandButton value="delete" action="#{userController.delete(user)}"> 
        <f:ajax render=":usersForm:usersTable"/> 
       </h:commandButton> 
      </h:column> 
     </h:dataTable> 
    </h:form> 

它仍然不更新。額外的細節我還有一個過濾器的形式,做更新,當它被付諸行動表形式以外的位,這裏是完整的JSF

<h:form id="filterForm"> 
     <h:selectBooleanCheckbox id="selectAll" value="#{userController.userFilter.allUsers}" title="allUsers"> 
      <f:ajax render="filterGrid usersForm" listener="#{userController.listAllUsers}"/> 
     </h:selectBooleanCheckbox><h:outputText value ="All users"/> 
     <h:panelGrid id="filterGrid" columns="3"> 
      <h:inputText id="userName" value="#{userController.userFilter.userName}" disabled="#{userController.userFilter.allUsers}"/> 
      <h:selectOneMenu id="selectLocation" value="#{userController.userFilter.location}" disabled="#{userController.userFilter.allUsers}"> 
       <f:selectItems value="#{userController.locations}" var="location" itemValue="#{location.location}" itemLabel="#{location.location}"/> 
      </h:selectOneMenu> 
      <h:commandButton id="filterButton" value="Filter" disabled="#{userController.userFilter.allUsers}" action="#{userController.findUsers()}"/> 
     </h:panelGrid> 
    </h:form> 
    <h:form id="usersForm" rendered="#{not empty userController.users}"> 
     <h:dataTable id="usersTable" value="#{userController.users}" var="user"> 
      <h:column>#{user.name}</h:column> 
      <h:column>#{user.location.location}</h:column> 
      <h:column> 
       <h:commandButton value="delete" action="#{userController.delete(user)}"> 
        <f:ajax render=":usersForm:usersTable"/> 
       </h:commandButton> 
      </h:column> 
     </h:dataTable> 
    </h:form> 

而且控制器豆

@Named 
@ViewScoped 
public class UserController implements Serializable 
{ 

    @Inject 
    private UserService userService;  

    private List<User> users;  

    private User user; 

    /** 
    * Init method used to initialise users list 
    */ 
    @PostConstruct 
    public void init() 
    { 
     users = userService.listAll(); 
    } 

    /** 
    * Delete the specified user 
    * 
    * @param user User to be deleted 
    */ 
    public void delete(User user) 
    { 
     userService.deleteUser(user); 
    }   

} 

,服務

@Stateless 
public class UserService 
{ 

    @PersistenceContext(unitName = "UsersJSFApplicationPU") 
    private EntityManager em; 

    /** 
    * Deletes the specified user from the database 
    * 
    * @param user to delete 
    */ 
    public void deleteUser(User user) 
    { 
     User usr = em.find(User.class, user.getId()); 
     em.remove(usr); 
    } 

    /** 
    * Returns a list of all users 
    * 
    * @return user list 
    */ 
    public List<User> listAll() 
    { 
     return em.createQuery("SELECT u from User as u").getResultList(); 
    } 

} 
+0

我不確定它是否完全相同,我查看了生成的html,它有'

',所以我將ajax標記更新爲''和我有同樣的事情,沒有更新 – PDStat

+0

增加了一些細節,你需要更多?就像我說的刪除工作,但表單不會更新。 – PDStat

+0

不,它似乎有此組件的更新元素的一部分' PDStat

回答

0

哎呀學校的男孩錯誤,則控制器刪除方法是錯誤的,必須

/** 
* Delete the specified user 
* 
* @param user User to be deleted 
*/ 
public void delete(User user) 
{ 
    userService.deleteUser(user); 
    users = userService.listAll(); 
}