0

Is it possible to reference an id, that identifies a component that is a child of the parents-parent-naming container?

For example:

<div class="browserDiv"
            id="browserObjects">
            <p:selectOneRadio id ="selectedFolder" value="${cc.attrs.value.selectedObjectPath}" layout="custom">
                <f:selectItems value="${cc.attrs.value.objectList}"
                                var="object" itmeValue="${object.path}" itemLabel=""/>
            </p:selectOneRadio>

            <table>
                <ui:repeat var="object" value="${cc.attrs.value.objectList}" id ="repeat" varStatus="status" >
                        <tr>
                            <td class="checkBoxCell">
                                <p:radioButton id="radioButton_${object.path}" for="selectedFolder" itemIndex="#{status.index}"/>
                            </td>
                        </tr>
                </ui:repeat>
            </table>

        </div>

I want to reference the id selectedFolder from the for-attribute in <p:radioButton>. But since <ui:repeat> is a NamingContainer, selectedFolder lies in another NamingContainer and I don't see how this can be referenced. Is it possible to write something like for="../selectedFolder"?

I cannot use absolute id-references because this is part of a composite component. I also tried using prependId="false" in <ui:repeat>, but that didn't work.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
treeno
  • 2,510
  • 1
  • 20
  • 36

1 Answers1

0

Use an absolute client ID instead of a relative client ID.

Assuming that you've a

<h:form id="form">
    <p:selectOneRadio id="radio" ...>
        ...
    </p:selectOneRadio>
</h:form>

then its absolute client ID would be form:radio. It's exactly the value which you see in the generated HTML source. To reference it from inside another naming container, you need to prefix it with the (default) naming container separator : so that it becomes :form:radio. So, this should do:

<p:radioButton for=":form:radio" ... />

Unrelated to the concrete problem, you're using ${} instead of #{} almost everywhere. While ${} may work fine in display-only values, it won't work at all whenever you submit the form. You'd really need #{} instead. It does not only do get, but also does set, while ${} only does get. Just stick to #{} throughout all JSF pages.

See also:


Update as per the comments, it's unclear where the naming containers are all placed in the composite, but if necessary you can resolve the absolute client ID of the composite dynamically as follows:

<p:radioButton for=":#{cc.clientId}:radio" ... />
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • how can I use absolute ids within a compsite component? – treeno Jan 16 '12 at 15:23
  • You can use `#{cc.clientId}` to dynamically obtain the absolute client ID of the current composite. You can use `#{cc.parent.namingContainer.clientId}` to obtain the absolute client ID of the parent naming container holding the current composite. See what fits the best and do the math. – BalusC Jan 16 '12 at 15:26
  • Thank you so far, but cc.parent.namingContainer.clientId doesn't return anything in my case. The cc is child of a panelGrid and a form... I thought those components are namingcontainer... – treeno Jan 16 '12 at 16:18
  • The panel grid is not a naming container. Check the list of "known implementing classes" in javadoc of `NamingContainer` to see which components are: http://docs.oracle.com/javaee/6/api/javax/faces/component/NamingContainer.html. – BalusC Jan 16 '12 at 16:22
  • The strange thing is, even when i hardcode the reference and copy the id from the generated html, it doesn't work... – treeno Jan 16 '12 at 16:34
  • Given the fact that you asked [this other question](http://stackoverflow.com/questions/8877418/composite-component-with-child-composite-component-not-thread-safe) I start to think that your problem is in the way how you developed, definied and referenced the composites. Please answer my comment in that other question. At least, your current question is now unanswerable until you show the smallest possible but *complete* copy'n'paste'n'runnable snippets which allows us to reproduce exactly your problem without any additional code. – BalusC Jan 16 '12 at 17:13
  • unfortunatly I don't have the time to build a c&p example right now. I "solved" the problem by passing the absolute composite components parent-id into the composite component. Then I can concat the clientId of the component within the composite component with the prefix so that I have an absolute id. This is more a workaround, ther must be a better way... I hope I find the time to work for a better solution... – treeno Jan 20 '12 at 10:14