コンポーネントバインディングとclientIdプロパティを使用して、JavaScriptコード内のコンポーネントを検索します。次のようになります。
<div data-for="@parent" data-example="#{componentBindings.myInput.clientId}" />
...
<h:inputText binding="#{componentBindings.myInput}" value="#{myBean.myProperty}" />
これに関する唯一の問題は、jsfがinputTextフィールドのid属性をレンダリングしないことがあるため、jsコードでclientIdを知っていても、クライアント側にid属性がないため、要素を見つけることができないことです。
- 自動生成されたidタグをレンダリングするための要件は何ですか?
- jsfにすべてのcliendIdを自動的にレンダリングさせる方法はありますか?(faces-configエントリのように)
- 個々の要素のclientIdが(明示的にIDを指定せずに)レンダリングされるようにする方法はありますか?
アップデート
私はトリックを使って自分の道を見つけました:
<span id="#{componentBindings.myInput.clientId}">
<h:inputText binding="#{componentBindings.myInput}" value="#{myBean.myProperty}" />
</span>
この場合、提案されているように名前を使用するなど、より良い方法がありますが、ajaxでレンダリングする入力フィールドでない場合は、便利な場合があります。たとえば、ファセットの場合:
<!-- myComponent.xhtml -->
<!-- ... -->
<cc:interface>
<cc:facet name="someFacet" required="true" />
</cc:interface>
<cc:implementation>
<h:form>
<cc:renderFacet name="someFacet"/>
</h:form>
</cc:implementation>
<!-- page.xhtml -->
<!-- ... -->
<my:myComponent>
<f:facet name="someFacet">
<h:inputText />
<!-- ... -->
</f:facet>
</my:myComponent>
コンポーネントツリーを見ると、ファセットがフォームにネストされておらず、ccの直接の子であることがわかります。したがって、ajaxを使用してccを実行すると便利です。
<!-- myComponent.xhtml -->
<!-- ... -->
<cc:interface>
<cc:facet name="someFacet" required="true" />
</cc:interface>
<cc:implementation>
<h:form>
<cc:renderFacet name="someFacet"/>
<h:commandLink>
<f:ajax execute=":#{cc.clientId}" render=":#{cc.clientId}" />
</h:commandLink>
</h:form>
</cc:implementation>
ただし、クライアント側に要求されたIDを持つ要素がないため、これは不正な形式のxml例外をスローします。このトリックを使用すると、それを機能させることができます:
<cc:interface>
<cc:facet name="someFacet" required="true" preferred="true"/>
</cc:interface>
<cc:implementation>
<span id=#{cc.clientId}>
<h:form>
<cc:renderFacet name="someFacet"/>
<h:commandLink>
<f:ajax execute=":#{cc.clientId}" render=":#{cc.clientId}" />
</h:commandLink>
</h:form>
</span>
</cc:implementation>
私はおそらくこの回避策を見つけた最初の人ではありませんが、どこにも見つからなかったので共有することにしました