1

I have a button per row in a h:dataTable that deletes the record. The action works as it deletes the record. But the table doesn't update, I'm not sure what's wrong? Here's the 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>

Edit: I don't believe the answer pointed to is quite the same, after look at the answer I found the generated html had the table defined as <table id="usersForm:usersTable"> So I modified the JSF as follows

    <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>

It still doesn't update. A bit of additional detail outside of the form I have another filter form that does update the table when it is actioned, here is the complete 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>

And the controller bean

@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);
    }        

}

And the service

@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();
    }

}
PDStat
  • 5,513
  • 10
  • 51
  • 86
  • I'm not sure if it is quite the same, I took a look at the generated html and it had `
    ` and I had the same thing, no update
    – PDStat Jul 02 '15 at 08:29
  • Added a bit more detail, do you need even more? Like I said the delete does work, but the form doesn't update. – PDStat Jul 02 '15 at 08:42
  • No it seems to have this component as part of it's update element ` – PDStat Jul 02 '15 at 08:48
  • Ok I've added the controller and service code, also removed the irrelevant stuff, so kept it to the deletion and listing of users – PDStat Jul 02 '15 at 08:59

1 Answers1

0

Argh school boy error, the controller delete method was wrong, needs to be

/**
 * Delete the specified user
 *
 * @param user User to be deleted
 */
public void delete(User user)
{
    userService.deleteUser(user);
    users = userService.listAll();
}        
PDStat
  • 5,513
  • 10
  • 51
  • 86