7
votes

I am using PrimeFaces 3.5 and I am implementing the DataTable - ContextMenu component. However, I want to delete a row by clicking on one cell in the row.

My JSF page:

<h:form id="form">

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

    <p:contextMenu for="productID" widgetVar="cMenu">
        <p:menuitem value="Edit Cell" icon="ui-icon-search"
                    onclick="productService.showCellEditor();return false;" />
        <p:menuitem value="Delete Row" icon="ui-icon-search"
                    onclick="productService.delete();return false;" />
    </p:contextMenu>

    <p:dataTable id="productID" var="product"
                 value="#{productService.getListOrderedByDate()}"
                 editable="true" editMode="cell" widgetVar="productTable"
                 paginator="true" rows="10"
                 paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                 rowsPerPageTemplate="5,10,15">

        <f:facet name="header"> Airbnb Product  </f:facet>

        <p:ajax event="cellEdit"
                listener="#{productService.onCellEdit}"
                update=":form:messages" />

        <p:column headerText="id" style="width:15%">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{product.id}" />
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{product.id}" style="width:96%" />
                </f:facet>
            </p:cellEditor>
        ...
        </p:column>
    </p:dataTable>
</h:form>

I have implemented a delete method in my service which should work. However, when I press the delete button in my context menu nothing happens. What could the cause of the problem be?

UPDATE

I did it like on the PF Page with:

public void delete() {
    log.info("Deleting data:");
    log.info(selectedRow.getId());
//  productDAO.delete(selectedRow);

}

and my datatable:

                <p:contextMenu for="productID" widgetVar="cMenu">
                    <p:menuitem value="Edit Cell" icon="ui-icon-search"
                        onclick="productService.showCellEditor();return false;" />
                    <p:menuitem value="Delete" update="productID" icon="ui-icon-close"
                        actionListener="#{productService.delete}" />
                </p:contextMenu>

                <p:dataTable id="productID" var="product"
                    value="#{productService.getListOrderedByDate()}"
                    editable="true" editMode="cell" widgetVar="productTable"
                    paginator="true" rows="10"
                    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                    rowsPerPageTemplate="5,10,15"
                    selection="#{productService.selectedRow}" selectionMode="single" rowKey="#{product.id}">

However, when I want to get the ID from selected Row I get a NullPointerException. I really appreciate your answer!!

1
is it a java bean method productService.delete(); you are trying to execute in your js onclick callback? If it is , you can't do it like thisDaniel

1 Answers

7
votes

Here is one way to delete a row using context menu...

<p:menuitem value="Delete" update="productID" icon="ui-icon-close" action="#{productService.delete}"/>  

Add to your table :

<p:dataTable selection="#{productService.selectedRow}" selectionMode="single"....

In your bean

public void delete() {  
    carsSmall.remove(selectedRow);  
}  

//declare selectedRow variable + getter/setter

Took it from the primefaces showcase : DataTable - ContextMenu