Valla
Valla

Reputation: 2442

table not refreshing after deleting a row in primefaces p:datatable

So I hava table which has 4 columns.The 4th columns has a button which deletes the record,when the button is cliked a modal dialog opens up and then it asks for confirmation Yes (or) NO.So when I click on Yes the record gets deleted from the database and also the modal dilog closes but the table is not getting refreshed.If I manually refresh it the record does not show.

This is my code in JSF for the modal dialog:

<p:dialog id="modalDialog" header="Delete User?"  modal="true"
resizable="false" draggable="false" widgetVar="delUserConf">  
  <h:panelGrid id="display" columns="2">
   <h:outputLabel value="Name" style="font-weight: bold"/>
   <h:outputText value="#{userController.selectedBean.firstName}" style="border: none"/>
 </h:panelGrid>
 <p:commandButton oncomplete='delUserConf.hide();' actionListener="#{mybeanr.deleteUser(userController.selectedBean)}"  id="yesDeleteUser" value="#{bundle.ListUserDeleteYes}" update=":userForm"/>
 <p:commandButton id="noDelete" onclick="delUserConf.hide();" action="/views/user/UserManagement.xhtml" value="#{bundle.ListUserDeleteNo}" style="margin-left: 15px;" update=":userForm:dataTab"/>
</p:dialog>

where 'userForm' is the if of the form. The dialog button is not in the p:datatable.My code in the bean:

public String deleteUser(UserBean userbean) {
 //do something
 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();

 if(//condiition checking )
 {
     JsfUtil.addErrorMessage("Not Allowed");
 }
 else
 {
   try {
       //some logic here
       JsfUtil.addSuccessMessage("Success");
   } catch (Exception e) {
       JsfUtil.addErrorMessage("Error");
   }
   //performDestroy();
   recreatePagination();
   recreateModel();
 }
 return "UserDetails";
}

Datatable and form code:

<h:form id="userForm">
 <p:growl id="growl"  showDetail="true" sticky="true" />
  <div class="dataTableWrap">
   <p:dataTable value="#{mybean.usersList}" var="item" id="dataTab" widgetVar="usersTable" editable="true" rowKey="#{item.emailAddress}">
   have some columns here and just displaying the values of these columns from the beans.
  </p:datatable>
</h:form>

UserDetails is the jsf page where I have this table.If you notice I am returning few messages from the bean.This is working fine but I do not understand why the table is not getting refreshed.Could anyone let me know why?

Upvotes: 0

Views: 5018

Answers (2)

andremonteiro
andremonteiro

Reputation: 476

Are you updating the mybean.usersList? I don't know what recreatePagination() and recreateModel() do, but, since you are not using lazy model, try to remove the deleted userbean from the list after you perform destroy. Something like

public String deleteUser(UserBean userbean) {
 //do something
 selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();

 if(//condiition checking )
 {
     JsfUtil.addErrorMessage("Not Allowed");
 }
 else
 {
   try {
       //some logic here
       JsfUtil.addSuccessMessage("Success");
   } catch (Exception e) {
       JsfUtil.addErrorMessage("Error");
   }
   //performDestroy();

   usersList.remove(userBean);

   recreatePagination();
   recreateModel();
 }
 return "UserDetails";
}

Upvotes: 1

Johnson Eyo
Johnson Eyo

Reputation: 113

when deleting from an entity from the jsf uifront end,the entity is not in a managed state, so u need to merge back to obtain the managed object, and now delete using the emcreateQuery("") to delete where entity object exists on table, or u can do geList().remove(entity) from jsf ui . The first options deletes from the db instantly refreshing the page and the second option deletes the merged entity and also removes it from the ui list

Upvotes: 0

Related Questions