删除PrimeFaces数据表中的行 [英] Delete row in a PrimeFaces datatable

查看:70
本文介绍了删除PrimeFaces数据表中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经在我的服务中实现了删除方法,该方法应该可以工作.但是,当我按上下文菜单中的删除按钮时,什么也没有发生.问题的原因可能是什么?

更新

我在PF页面上的操作方式如下:

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

}

和我的数据表:

                <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.非常感谢您的回答!

解决方案

这是使用上下文菜单删除行的一种方法...

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

添加到表格中

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

在您的豆子中

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

//declare selectedRow variable + getter/setter

从primefaces展示柜中获取: DataTable-ContextMenu

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

解决方案

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

这篇关于删除PrimeFaces数据表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆