I was able after many tries that the lazy load works...in the table there´re two colmuns that are used for delete and edit the selected row...edit works fine..but I´m having problems when select the row for delete...in fact the data is deleted from the database but the datatable isn't refresh..it still contain the same data..if i click in the pagination buttons, and after I come back to the page that contain the removing row, effectively the row has been removed...
My question is how to make for update de datatable at the moment that the row is deleted?
Code for my datatable
Code: Select all
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title></title>
</h:head>
<h:body>
<h:form id="mainForm">
<p:dataTable id="clientesTable"
value="#{cliente.clienteLazy}" lazy="true" var="row"
paginator="true" rows="10" rowIndexVar="rowIdx">
<p:column>
<f:facet name="header">
<h:outputText value="Nombre"/>
</f:facet>
<h:outputText value="#{row.nombre} #{row.apellido}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Eliminar"/>
</f:facet>
<p:commandButton styleClass="button_table_size" image="button_delete_icon" onclick="confirmation.show()">
<f:setPropertyActionListener value="#{row}" target="#{cliente.selectedCliente}"/>
</p:commandButton>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Editar"/>
</f:facet>
<p:commandButton update="dialogForm:datosAEditar" image="button_edit_icon" styleClass="button_table_size"
oncomplete="editDialog.show()">
<f:setPropertyActionListener value="#{row}" target="#{cliente.selectedCliente}"/>
</p:commandButton>
</p:column>
</p:dataTable>
<h:panelGrid columns="1">
<p:commandButton value="Nuevo" onclick="newDialog.show()"/>
</h:panelGrid>
</h:form>
<!-- ************************Dialog de confirmacion de eliminacion de cliente ******************************-->
<p:confirmDialog message="Seguro que desea eliminar el cliente?"
showEffect="bounce" hideEffect="explode"
header="Eliminar" severity="alert" widgetVar="confirmation">
<h:form id="deleteDialogForm">
<p:commandButton value="Si" actionListener="#{cliente.deleteSelectedCliente}" styleClass="button"
update="mainForm:clientesTable" oncomplete="confirmation.hide()"/>
<p:commandButton value="No" onclick="confirmation.hide()" type="button" styleClass="button"/>
</h:form>
</p:confirmDialog>
<!-- ************************Dialog de edicion de cliente ******************************-->
<p:dialog header="Editar Usuario" widgetVar="editDialog" closable="true"
showEffect="slide" hideEffect="fold" modal="true">
<h:form id="dialogForm">
<p:outputPanel id="datosAEditar" rendered="true">
<h:panelGrid columns="2" >
<h:outputText value="Nombre:"/><h:inputText value="#{cliente.selectedCliente.nombre}"/>
<h:outputText value="Apellido:"/><h:inputText value="#{cliente.selectedCliente.apellido}"/>
<h:outputText value="Dni:"/><h:inputText value="#{cliente.selectedCliente.dni}"/>
<h:outputText value="Telefono:"/><h:inputText value="#{cliente.selectedCliente.telefono}"/>
<h:outputText value="Mail:"/><h:inputText value="#{cliente.selectedCliente.mail}"/>
<h:selectBooleanCheckbox value="#{cliente.selectedCliente.estudiante}"/><h:outputText value="Estudiante?"/>
<p:commandButton styleClass="button" value="Aceptar" actionListener="#{cliente.editCliente}"
update="mainForm:clientesTable" oncomplete="editDialog.hide()"/>
<p:commandButton styleClass="button" value="Cancelar" onclick="editDialog.hide()"/>
</h:panelGrid>
</p:outputPanel>
</h:form>
</p:dialog>
</h:body>
</html>
Code: Select all
@ManagedBean(name="cliente")
@ViewScoped
public class ClienteBeanTest extends LazyDataModel<Cliente> implements Serializable {
private LazyDataModel<Cliente> clienteLazy;
private Cliente selectedCliente;
private Cliente cliente;
public Cliente getSelectedCliente() {
return selectedCliente;
}
public void setSelectedCliente(Cliente selectedCliente) {
this.selectedCliente = selectedCliente;
}
public ClienteBeanTest(){}
public LazyDataModel getClienteLazy(){
this.setRowCount(ClienteDAO.count());
return this;
}
public void setClienteLazy(LazyDataModel<Cliente> clienteLazy) {
this.clienteLazy = clienteLazy;
}
@Override
public List<Cliente> load(int first, int pageSize, String string, boolean bln, Map<String, String> map) {
return ClienteDAO.getList(Cliente.class, first, pageSize);
}
public void deleteSelectedCliente(ActionEvent evt){
Transaction t= GenericDAO.createTransaction();
GenericDAO.delete(selectedCliente);
t.commit();
}
public void editCliente(ActionEvent evt){
Transaction t= GenericDAO.createTransaction();
GenericDAO.saveOrUpdate(selectedCliente);
t.commit();
}
public void saveCliente(ActionEvent evt){
try
{
Transaction t = GenericDAO.createTransaction();
GenericDAO.saveOrUpdate(cliente);
GenericDAO.commit(t);
}
catch (org.hibernate.exception.ConstraintViolationException e)
{
}
catch (Exception e)
}
}
}