如何解决一个循环命名容器内的成分 [英] How to address a component inside a looping naming container

查看:170
本文介绍了如何解决一个循环命名容器内的成分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构(内容和属性省略):

I have the following structure (contents and attributes omitted):

<ui:repeat id="outerlist">
    <my:compositeComponent id="myCC">
        <h:panelgroup id="container">
            Some content here (outputText, etc.)
            <ui:repeat id="innerlist">
               <h:commandButton>
                   <f:ajax render=":#{cc.clientId}:container" />

<!-- all closing tags accordingly -->

由于容器内的内容取决于innerlist的按钮的操作,我需要更新它。如上图所示的作品,在没有外 UI的方法:重复。然而,它不能用成分没有找到错误时有一个。

As the content inside the container depends on the action of the innerlist's button, I need to update it. The approach as shown above works, when there is no outer ui:repeat. However, it fails with a component not found error when there is one.

这是似乎由于该 cc.clientId 则本身就含有外 UI的行索引的事实:重复,例如: outerlist:0:myCC:集装箱。作为评论这个答案表示,这个索引ID不在的服务器端再presentation可用视图树。相反,行索引只在客户端的存在。我必须承认,我不太了解这个索引是如何完成的,什么是可在服务器端。

This is seems due to the fact that the cc.clientId then itself contains the row index of the outer ui:repeat, e.g. outerlist:0:myCC:container. As a comment to this answer indicates, this indexed ID is not available in the server-side representation of the view tree. Instead "the row index only exist at client side". I must admit I do not quite understand how this indexing is done and what is available at the server side.

所以我的问题是:如何JSF做索引,它如何(在服务器上)独立的不同的实例在 UI:重复,是有解决方案是什么,我想实现上述code?

So my question is: How does JSF do this indexing, how does it (on the server) separate different "instances" inside a ui:repeat and is there a solution for what I am trying to achieve with the above code?

推荐答案

&LT指定的客户端ID; F:AJAX&GT; 必须在两者都可的由JSF的服务器端

The client ID as specified in <f:ajax> must be available in both the server side by JSF's

facesContext.getViewRoot().findComponent(clientId);

(这样就可以以呈现其新的HTML重新presentation的Ajax响应中找到)

通过JavaScript的

and in the client side by JavaScript's

document.getElementById(clientId);

(这样就可以被更新/被JS更换一次新的HTML内容的Ajax响应已到达)

由于&LT; UI:重复&GT; 在视图运行渲染时止,具有行索引的客户端ID不会重新present在服务器的有效成分方(无法找到组件...错误从 findComponent()),但它并重新present在客户端的有效HTML元素。基本上,你需要在客户端ID,而无需行索引服务器端和一个与行索引为客户端。不过,就不会为<$​​ C $ C>&LT工作; UI:重复&GT; ,因为它是(不幸)不可能仅通过<$ c选择一个特定迭代轮组件树状态$ C> findComponent()。

As the <ui:repeat> runs during view render time only, the client ID with the row index does not represent a valid component in server side ("Cannot find component..." error from findComponent()), but it does represent a valid HTML element in the client side. Basically, you'd need the client ID without the row index for the server side and the one with the row index for the client side. But that just won't work for <ui:repeat> as it's (unfortunately) not possible to select the component tree state of a specific iteration round by alone findComponent().

应该使用JSTL时正常工作 c为C:&GT的forEach; 和动态分配组件ID,因为它在视图生成时运行,实际上也产生多种fullworthy JSF组件视图树,而不是只有一个期间呈现该被再使用多次。

It should work fine when using JSTL <c:forEach> and dynamically assigning component ID as it runs during view build time and also actually generates multiple fullworthy JSF components in the view tree instead of only one which is re-used multiple times during render.

<c:forEach varStatus="loop">
    <my:compositeComponent id="myCC">
        <h:panelGroup id="container_#{loop.index}">
            Some content here (outputText, etc.)
            <ui:repeat id="innerlist_#{loop.index}">
               <h:commandButton>
                   <f:ajax render=":#{cc.clientId}:container_#{loop.index}" />

然而,这有它自己的意义,复合组件使用,还嵌套循环使用时肯定时。您的code是不完整的,足以让有关的见解和建议。当这片code为放置在复合元件是由本身就成了例如打破中也有呈现时循环多次重复使用。

This has however its own implications, certainly when used with composite components and also when used in nested loops. Your code is not complete enough to give insight and advice about that. It would for example break when this piece of code is placed in a composite component which is by itself also reused multiple times in a render time loop.

  • <一个href="http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense/3343681#3343681">JSTL在JSF2 Facelets的......有道理?
  • <一个href="http://stackoverflow.com/questions/8634156/how-to-reference-components-in-jsf-ajax-cannot-find-component-with-identifier/8644762#8644762">How引用组件的JSF AJAX?无法找到标识符QUOT组成部分;富&QUOT;鉴于
  • JSTL in JSF2 Facelets... makes sense?
  • How to reference components in JSF ajax? Cannot find component with identifier "foo" in view

这篇关于如何解决一个循环命名容器内的成分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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