参考基于迭代索引的JSF动态生成的ID [英] Refer to JSF dynamically generated ids based on iteration index

查看:155
本文介绍了参考基于迭代索引的JSF动态生成的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSF中,<ui:repeat/>和类似的组件(例如PrimeFaces <p:dataTable/>)会基于迭代索引为子组件生成动态ID,即:

In JSF, <ui:repeat/> and similar components such as PrimeFaces <p:dataTable/> generate dynamic ids for sub-components based on the iteration index, i.e.:

<p:dataTable id="table" var="item" value="#{itemList}">
    <h:outputText id="name" value="#{item.name}"/>
</p:dataTable>

将生成如下内容:

<table id="table">
    <span id="table:0:name">name0</span>
    <span id="table:1:name">name1</span>
    <span id="table:2:name">name2</span>
    ...
    <span id="table:n:name">nameN</span>
</table>

因此,所有元素显然都具有不同的客户端ID.我故意跳过了<tr/><td/>

so all the elements clearly have a distinct client id. I intentionally skipped the <tr/>, <td/>, etc.

因此,<p:ajax ... update=":table:name"/>引用了表中的所有名称,并且可以正常工作,尽管我可以确认标记中存在该组件,但是<p:ajax ... update=":table:#{someDesiredIndex}:name"/>失败并显示类似于SEVERE: javax.faces.FacesException: Cannot find component with identifier ":table:0:name" in view.事件的消息.不可能这样做吗?

So, <p:ajax ... update=":table:name"/> refers to all the names in the table and it works fine, <p:ajax ... update=":table:#{someDesiredIndex}:name"/> fails with a message similar to SEVERE: javax.faces.FacesException: Cannot find component with identifier ":table:0:name" in view. event though I can confirm that the component exists in the markup. Is it not possible to do this?

在相关的情况下,我正在运行GlassFish 3.1.2和Mojarra 2.1.6.

I'm running on GlassFish 3.1.2 and Mojarra 2.1.6 in case it is relevant.

推荐答案

JSF组件树中确实不存在它,因为,而不像您期望的那样多.生成HTML输出时,它只是被多次重用.充其量,您可以通过table:name获取物理组件,但是HTML DOM树中却不存在该物理组件,因此document.getElementById()在执行ajax更新时会失败.

It does indeed not exist in the JSF component tree as traversable by UIViewRoot#findComponent(). It exists only in the generated HTML output. There's only one <h:outputText id="name"> in the JSF component tree, not multiple as you seemed to expect. It's just been reused multiple times when producing the HTML output. At best, you can get the physical component by table:name, but this does in turn not exist in the HTML DOM tree, so the document.getElementById() would fail on that during performing the ajax update.

无论如何,为了实现具体的功能要求,您基本上需要有一个物理上存在的组件来表示JSF组件树中的行.如果使用视图构建时间标签(例如JSTL <c:forEach>)而不是视图呈现时间标签,则可以在循环中创建它们.

In order to achieve the concrete functional requirement anyway, you basically need to have a physical existing component representing the row in the JSF component tree. You can create them in a loop if you use a view build time tag, such as JSTL <c:forEach>, instead of a view render time tag.

<table id="table">
    <c:forEach items="#{itemList}" var="item" varStatus="loop">
        <tr><td><h:outputText id="table_#{loop.index}_name" value="#{item.name}" /></td></tr>
    </c:forEach>
</table>

这将在JSF组件树中实际创建多个组件,并将其呈现为:

This will create physically multiple components in the JSF component tree and this get rendered as:

<table id="table">
    <span id="table_0_name">name0</span>
    <span id="table_1_name">name1</span>
    <span id="table_2_name">name2</span>
    ...
    <span id="table_n_name">nameN</span>
</table>

您可以通过例如引用它们update=":table_#{someDesiredIndex}_name".

And you can reference them via e.g. update=":table_#{someDesiredIndex}_name".

  • How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"
  • JSTL in JSF2 Facelets... makes sense?

更新:自Mojarra 2.2.5起,<f:ajax>不再验证客户端ID,并且渲染器能够遍历迭代组件以找到合适的迭代轮来进行渲染.因此,以这种方式在<f:ajax>中引用迭代索引应该可以正常工作.它仅在当前的MyFaces 2.2.7/PrimeFaces 5.1版本中尚不可用,但预计他们会在将来的版本中赶上它.

Update: since Mojarra 2.2.5, the <f:ajax> doesn't validate the client ID anymore and the renderer is capable of walking through iterating components in order to find the right iteration round to render. So referencing the iteration index in <f:ajax> this way should just work fine. It only doesn't work yet in current MyFaces 2.2.7 / PrimeFaces 5.1 versions, but it's expected that they will catch up it in a future version.

这篇关于参考基于迭代索引的JSF动态生成的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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