I can not remove the object from my list DataTable

2

I am inside my service and here is where I put the parameter so that I receive the id of the row I want to delete.

@Transactional
public void eliminaHerramienta(Integer idherramienta) {
    try {
        Herramienta p = manager.find(Herramienta.class, idherramienta);
        manager.getTransaction().begin();
        manager.remove(p);
        manager.flush();
        manager.getTransaction().commit();
    } catch (Exception e) {
        manager.getTransaction().rollback();
        e.printStackTrace();
    } finally {
        manager.close();
    }
}

Then I'm in the controller to do the interaction with the view which is like a requestScoped

@Transactional
public void eliminar(Integer idherramienta) {
    System.out.println("entrando a eliminar alumno");
    herramientaService.eliminaHerramienta(idherramienta);

}

apparently everything is great but it does not go into the method of removing the moment when I click on the button to call that action on the controller.

    <p:dataTable value="#{herramientaAction.lstherramienta}" var="h"
        border="1" paginator="true" rows="5">
        <p:column>
            <f:facet name="header">Id</f:facet>
                        #{h.idherramienta}
                    </p:column>
        <p:column>
            <f:facet name="header">Nombre</f:facet>
                        #{h.name}
                    </p:column>
        <p:column>
            <f:facet name="header">stock</f:facet>
                        #{h.stock}
                    </p:column>
        <p:column>
            <f:facet name="header">precio</f:facet>
                        #{h.precio}
                    </p:column>
        <p:column>
            <f:facet name="header">fecha vencimiento</f:facet>
                        #{h.fecha}
                    </p:column>
        <p:column>
            <f:facet name="header">modelo</f:facet>
                        #{h.modelo.descripcion}
                    </p:column>

        <p:column>
            <p:commandButton id="btn1"
                action="#{herramientaAction.eliminar(h.idherramienta)}"   value="eliminar" />
        </p:column>
    </p:dataTable>

As you can see it has the view of a datatable, I do not know what thing I have to add to work or receive that action.

    
asked by Sebastian Cordova 27.06.2018 в 23:15
source

1 answer

3

Your problem can be for several reasons:

1- Make sure that your datatable is within <h:form> since this is mandatory.

2- We are going to change the "action" in the commandbutton to "actionlistener". The action is more recommended when there is navigability, that is if you are going to change from one view to another or navigate to another site and it is not your case. Since you are going to be interested in doing an update of the table in the view when the delete action is performed.

3- To try to force the issue, add process="@this" , this way we indicate to JSF that it has to force the sending of the information to the server. If it still does not work, change to @form.

Note: Before doing these steps, put a break point in the delete method to make sure when your request arrives.

Tell me if it is solved with the above.

Greetings.

    
answered by 28.06.2018 / 16:00
source