jsf - Update form on h:commandButton action -
i have button per row in h:datatable
deletes record. action works deletes record. table doesn't update, i'm not sure what's wrong? here's 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: don't believe answer pointed quite same, after @ answer found generated html had table defined <table id="usersform:userstable">
modified jsf 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. bit of additional detail outside of form have filter form update table when actioned, here 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 controller bean
@named @viewscoped public class usercontroller implements serializable { @inject private userservice userservice; private list<user> users; private user user; /** * init method used initialise users list */ @postconstruct public void init() { users = userservice.listall(); } /** * delete specified user * * @param user user deleted */ public void delete(user user) { userservice.deleteuser(user); } }
and service
@stateless public class userservice { @persistencecontext(unitname = "usersjsfapplicationpu") private entitymanager em; /** * deletes specified user database * * @param user delete */ public void deleteuser(user user) { user usr = em.find(user.class, user.getid()); em.remove(usr); } /** * returns list of users * * @return user list */ public list<user> listall() { return em.createquery("select u user u").getresultlist(); } }
argh school boy error, controller delete method wrong, needs be
/** * delete specified user * * @param user user deleted */ public void delete(user user) { userservice.deleteuser(user); users = userservice.listall(); }
Comments
Post a Comment