primefaces - 从 p :dataTable 创建复合组件时出现“组件不支持事件”错误

标签 primefaces jsf-2 datatable

从 primefaces 数据表创建复合组件时出现以下错误。我做错了什么!?

Exception 

    javax.faces.view.facelets.TagException: /view/restrito/basico/municipio.xhtml @77,165 <p:ajax> Composite component does not support event rowSelect

tabela-padrao.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:cc="http://xmlns.jcp.org/jsf/composite"
  xmlns:c="http://java.sun.com/jsp/jstl/core"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core"
  xmlns:h="http://xmlns.jcp.org/jsf/html">

<!-- INTERFACE -->
<cc:interface>
    <cc:attribute name="uniqueId" required="true" />
    <cc:attribute name="value" required="true" />
    <cc:attribute name="var" required="true" />
    <cc:attribute name="selection" required="true" />
    <cc:attribute name="exportedFileName" required="true" />
    <cc:attribute name="renderedTable" default="true"/>
    <cc:attribute name="primaryKey" required="true"/>

</cc:interface>

<!-- IMPLEMENTATION -->
<cc:implementation>
    <p:dataTable value="#{cc.attrs.value}" 
                 id="#{cc.attrs.uniqueId}"
                 scrollable="true"
                 scrollWidth="100%"
                 var="#{cc.attrs.var}"
                 rendered="#{cc.attrs.renderedTable}"
                 selection="#{cc.attrs.selection}"
                 rowKey="#{cc.attrs.primaryKey}"                                             
                 selectionMode="single"
                 paginator="true"
                 rowsPerPageTemplate="15,30,45"
                 paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown} {Exporters}"
                 emptyMessage="#{bundle.tabela_nenhum_registro_encontrado}">
        <cc:insertChildren/>
        <f:facet name="{Exporters}">
            <h:commandLink style="padding: 5px 5px 5px 5px ;" title="Converter para Excel" >                                            
                <h:outputText  styleClass="fa fa-file-excel-o Fs20"/>
                <p:dataExporter type="xls" target="#{cc.attrs.uniqueId}" fileName="#{cc.attrs.exportedFileName}" />
            </h:commandLink>
            <h:commandLink style="padding: 5px 5px 5px 5px ;" title="Converter para PDF" >
                <h:outputText  styleClass="fa fa-file-pdf-o Fs20"/>
                <p:dataExporter type="pdf" target="#{cc.attrs.uniqueId}" fileName="#{cc.attrs.exportedFileName}"/>
            </h:commandLink>
        </f:facet>
    </p:dataTable> 
</cc:implementation>

使用复合组件/使用组件

<h:form id="tabela-municipio">
                    <ezcomp:tabela-padrao value="#{municipioMB.listaMunicipios}"
                                          uniqueId="id-tabela-municipio"
                                          var="mun" 
                                          primaryKey="#{mun.id}"
                                          selection="#{municipioMB.municipio}"
                                          exportedFileName="municipios">
                        <p:ajax event="rowSelect" listener="#{municipioMB.onRowSelect}" update="@(form[id*='frm-municipio']),@(form[id*='tabela-municipio'])" />
                        <p:ajax event="rowUnselect" listener="#{municipioMB.onRowUnselect}" update="@(form[id*='frm-municipio']),@(form[id*='tabela-municipio'])" />
                        <p:column headerText="Pais" width="300" filterBy="#{mun.estado.pais.nome}" filterMatchMode="contains">
                            <h:outputText value="#{mun.estado.pais.nome}"/>
                        </p:column>
                        <p:column headerText="Estado" width="300" filterBy="#{mun.estado.sigla} - #{mun.estado.nome}" filterMatchMode="contains">
                            <h:outputText value="#{mun.estado.sigla} - #{mun.estado.nome}"/>
                        </p:column>
                        <p:column headerText="Município" filterBy="#{mun.nome}" filterMatchMode="contains">
                            <h:outputText value="#{mun.nome}"/>
                        </p:column>
                    </ezcomp:tabela-padrao>
                </h:form>

最佳答案

您必须在复合组件中创建和注册自定义事件,并将相应的操作传递给数据表。由于 rowSelect 和 rowUnselect 事件是在数据表而不是复合组件上注册的。使用 clientBehavior 为复合组件注册事件。

<cc:interface>
    ...
    <cc:clientBehavior name="customRowSelectEvent" targets="idOfDataTable" event="rowSelect" />
    <cc:clientBehavior name="customRowUnselectEvent" targets="idOfDataTable" event="rowUnselect" />
</cc:interface>
  1. 名称是自定义事件的名称。
  2. targets 是您要为其实际注册操作的组件的 ID。在你的数据表的情况下。
  3. event 是您要为数据表注册的实际事件。

现在为复合组件注册事件。

<ezcomp:tabela-padrao ....>
    <f:ajax event="customRowSelectEvent" listener="#{municipioMB.onRowSelect}" update="@(form[id*='frm-municipio']),@(form[id*='tabela-municipio'])" />
    <f:ajax event="customRowUnselectEvent" listener="#{municipioMB.onRowUnselect}" update="@(form[id*='frm-municipio']),@(form[id*='tabela-municipio'])" />
      .....                  
</ezcomp:tabela-padrao>

关于primefaces - 从 p :dataTable 创建复合组件时出现“组件不支持事件”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43357559/

相关文章:

jsf - 尝试显示动态生成的条形码时显示上一个条形码

php - 我无法从我的代码中获得所需的结果

c# - 将 List<XElement> 转换为 DataTable

javascript - Jquery 数据表 : Replacing the buttons with icons at the same location

java - 更新页面以使用新命名空间 xmlns.jcp.org 后,JSF 标记未呈现

jsf-2 - 复合组件的 Primefaces 输出标签

jsf - p :signature 的较小显示

Primefaces p :menuitem with p:confirm not supported

mysql - 表更新和 Primefaces ScheduleEvent

jsf - 在 Web 应用程序中使用 EJB 有什么好处?