【问题标题】:Delete selected row datatable删除选定的行数据表
【发布时间】:2016-02-17 04:15:14
【问题描述】:

我有以下代码从 p:datatable 中删除选定的行

<p:commandLink id="deleteProp" action="#{locationBean.deleteProperty}" styleClass="datatabletext" update="locationProperties" process="@this">
 <h:graphicImage value="/resources/images/delete.gif" />
 <f:setPropertyActionListener target="#{locationBean.selectedProperty}" value="locProp" />
</p:commandLink>

豆码

public void deleteProperty() {
    System.out.println("selec  "+selectedProperty);
    locProps.remove(selectedProperty);
}

我也有 selectedProperty 的 getter 和 setter。但是当我点击删除链接时,我看到了以下错误。

WARNING: Cannot convert locProp of type class java.lang.String to class TO.LocationPropertiesTO
javax.el.ELException: Cannot convert locProp of type class java.lang.String to class TO.LocationPropertiesTO
at org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:416)

它甚至没有进入 action 方法。谁能告诉我我犯了什么错误?

当我将参数传递给 delete 方法并检查它是否正常工作时。但是为什么 f:setPropertyActionListener 失败了?

工作代码

  <p:commandLink id="deleteProp" rendered="#{fn:length(locationBean.locProps)>1}"
                                    action="#{locationBean.deleteProperty(locProp)}"
                                    styleClass="datatabletext" update="locationProperties"
                                    process="@this">
                                    <h:graphicImage value="/resources/images/delete.gif" />

</p:commandLink>

【问题讨论】:

  • selectedProperty 的类型是什么?
  • LocationPropertiesTO

标签: jsf primefaces


【解决方案1】:

我认为这与 PropertyActionListener 中缺少 EL 标签有关。

尝试使用:

<f:setPropertyActionListener target="#{locationBean.selectedProperty}" value="#{locProp}" />

这就是为什么 ELException 抱怨试图将字符串值解析到您的自定义 POJO(解析字符串“locProp”)。

【讨论】:

    【解决方案2】:

    你可以这样做:

    首先,将所选行的 ID 传递给您的 Bean,如下所示:

    <p:commandLink id="deleteProp" action="#{locationBean.deleteProperty}" styleClass="datatabletext" update="locationProperties" process="@this">
     <h:graphicImage value="/resources/images/delete.gif" />
     <f:setPropertyActionListener target="#{locationBean.selectedProperty}" value="locProp" />
    <f:param name="deletingRowId" value="#{row.id}" />
    </p:commandLink>
    

    row 是 DataTable 中的 var。

    <p:dataTable ... var="row" ... />
    

    然后,在您的 deleteProperty() 方法中,通过已传递的 Id 找到您的 LocationPropertiesTO 对象,如下所示:

    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    String id = params.get("deletingRowId");
    LocationPropertiesTO lo = findLObyId(id, locProps);
    locProps.remove(selectedProperty);
    

    最后,更新您的数据表。

    【讨论】:

    • 嗨。请查看我的编辑。我想知道为什么 f:setPropertyActionListener 失败
    • 似乎 &lt;p:dataTable ... var="locProp" ... /&gt; 合乎逻辑
    • 是的,我已经给出了。
    猜你喜欢
    • 1970-01-01
    • 2014-11-05
    • 2017-09-04
    • 2013-08-01
    • 2019-12-30
    • 2016-10-02
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多