2013-04-22 88 views
6

我正在使用PrimeFaces 3.5,并且正在实施DataTable - ContextMenu组件。 但是,我想通过单击行中的一个单元格来删除一行。在PrimeFaces数据表中删除行

我的JSF页面:

<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> 

我已经实现了我的服务的删除方法,它应该工作。但是,当我按下我的上下文菜单中的删除按钮时,没有任何反应。问题的原因是什么?

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

} 

,我的数据表:

UPDATE

我做了它喜欢上了PF页

   <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}"> 

然而,当我想要得到的ID从选择我得到一个NullPointerException。我真的很感谢你的回答!

+0

它是一个java bean方法'productService.delete();'你试图在你的js'onclick'回调中执行?如果是这样,你不能像这样做 – Daniel 2013-04-22 12:00:42

回答

7

下面是一个使用上下文菜单删除行的一种方式......

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

添加到您的表:

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

在你的bean

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

//declare selectedRow variable + getter/setter 

把它从primefaces展示:DataTable - ContextMenu

+0

thx为你的提示。但是,我这样实现它,但我得到一个'NullPointerException'(请参阅我的更新) – maximus 2013-04-22 15:20:48

+0

哪里是异常来自?你是否还可以确认每次尝试删除一行时都会更新'selectedRow'? – Daniel 2013-04-22 15:23:02

+0

异常来自'log.info(selectedRow.getId());'因为'selectedRow'是'Null'。该表应该由于'IDs'而被更新。但是,我不知道为什么它的'NULL'? – maximus 2013-04-22 15:28:20