How to delete a row in dataTable usin lazyLoad

UI Components for JSF
Post Reply
Jimmy87_XFaces
Posts: 10
Joined: 21 Dec 2010, 08:02

12 Jan 2011, 04:35

Hi..i have got an application and i´m using lazy load in a datatable because the data from de data bases are huge..

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 for my bean

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)
        }
    }
}
I hope yours answers :D :D :D :D
Last edited by Jimmy87_XFaces on 12 Jan 2011, 05:31, edited 1 time in total.

Jimmy87_XFaces
Posts: 10
Joined: 21 Dec 2010, 08:02

12 Jan 2011, 05:29

I have resolved my problem of update the dataTable after delete a row with this code:

DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("idForm:idTable");
dataTable.loadLazyData();

it´s called after remove the object from the database..

but i have another problem :| :| :( :( :| :| For example....if click en pagination buttons to the last page..and delete all rows in this page, after that remove de last row, the datatable shows me an empty table in the first page..if I click in the next button or in a page, it shows fine the data..
I have reading this in a post that seem that I need to make, but I don´t know how..
In 2.2, when a row is deleted from a lazy DataTable, there are a number of things that need to be performed on the server

- The rowCount for the LazyDataModel needs to be decremented by one.
- If the row that was deleted was on on the last page, and it was the only row on that page, the DataTables page and first properties need to be corrected, if appropriate.

The above will work correctly if there is a single user editing the data displayed by the DataTable. If many users can edit the data displayed by the DataTable at the same, all sorts of other terribe things can happen.
Any opinion???

jlferreira
Posts: 43
Joined: 16 Nov 2010, 17:52

11 Feb 2011, 21:18

Hi.
A way to better the problem of concurrency is, when the update/remove form is called, do a call to database and get the last informations of the called record.
But, the problem continue.
If you do a database lock, and show an error to other users who try to change the same record, you can minimize the problem too.
About the problem of remove the last record of the last page, I did as you said and the problem was minimized, but, nothing is showed in the current page. If the user press the next page button, he see the values of 2º page; from there, he can navigate normally.

The database lock, in my option, is the best choice.
Do you know how to do It?
Thanks...
Eclipse Helios.
JSF-2.0 (Mojarra 2.1.0-b11).
GlassFish 3.1.
PrimeFaces 2.2.1
EclipseLink 2.1.x - EclipseLink 2.1.x.
PostgreSQL 9.0

npmdharma
Posts: 2
Joined: 22 Mar 2013, 13:26

22 Mar 2013, 13:34

in my program i am using roweditor to edit row in datatable the problem is when is click edit option just the row hides and nothing happen

this is my code:


<h:form >

<p:panel id="panel" header="Student List..!!">
<p:inputText value="#{studentBean.classid}"/>
<p:commandButton action="#{studentBean.search1()}" value="view" update="staffList"/>

<p:dataTable var="staff" value="#{studentBean.adminstudentlist}" id="staffList" resizableColumns="true" editable="true" paginator="true" rows="5"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15" >

<f:facet name="header">
Student Profile
</f:facet>

<p:growl id="growl" showDetail="true"/>

<p:ajax event="rowEdit" listener="#{studentBean.onEdit}" update="staffList" />
<p:ajax event="rowEditCancel" listener="#{studentBean.onCancel}" update="staffList" />



<p:column headerText="UserName" width="100">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{staff.username}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{staff.username}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>



<p:column headerText="Options" width="100">
<p:rowEditor />
</p:column>


</p:dataTable>



</p:panel>
</h:form>

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

22 Mar 2013, 14:00

This post contains about 4 violations (out of 9) of the posting rules. Numbers 5, 6, 7 and 9 from the link in my signature....

MagicianOfTheWest
Posts: 2
Joined: 07 May 2013, 15:58

07 May 2013, 16:01

I HAVE THE SAME PROBLEM WITH MY PRIMEFACES VERSION

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

07 May 2013, 17:09

This post contains about 4.5 violations (out of 9) of the posting rules. Numbers 5, 6, 7, half of 8 and 9 from the link in my signature....

I'd personally change 8 to also include text, not only subjects, so it would be 5 out of 9...

MagicianOfTheWest
Posts: 2
Joined: 07 May 2013, 15:58

07 May 2013, 17:45

ANY IDEAS? COME ON IT'S URGENT - IN SHORT, I HAVE THE SAME PROBLEM. YOU'RE ALL PAID FULL TIME ENGINEERS, FIX MY PRIMEFACES VERSION PLEASE, IT'S NOT WORKING.

This question is not being read... :(

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

07 May 2013, 17:57

So you did read the forum posting rules...

But I reported this topic and you, so I think you'll be banned for a week.

Cheers

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 21 guests