簡體   English   中英

在render / update屬性中獲取模板中父命名容器的id

[英]Get id of parent naming container in template for in render / update attribute

我有一個模板,在其定義中我使用了幾個表單和按鈕。

問題是定義(定義)xhtml文件不知道組件層次結構。

例如,我想在同一個定義文件中以不同的形式更新元素“table2”。

模板插入:

<p:tabView id="nav"> <!-- nav -->
    <ui:insert name="content_nav">content navigation</ui:insert>
</p:tabView>

定義我的層次結構“nav”的第一級

模板定義:

<ui:define name="content_nav">
    <h:form id="form1"> <!-- nav:form1 -->
        <h:dataTable id="table1"/> <!-- nav:form1:table1 -->
        <p:inputText value="#{bean.value}"/>
        <p:commandButton action="..." update="nav:form2:table2"/>
    </h:form>
    <h:form id="form2">
        <h:dataTable id="table2"/> <!-- nav:form2:table2 -->
        <!-- other elements -->
    </h:form>
</ui:define>

在我的定義部分,我不想知道“導航”!

我怎樣才能做到這一點? 或者如何向上移動一個命名組件?或者將最高父完整ID保存在變量中?

有時我看到類似的東西:

update=":table2"

但是我找不到任何關於這個的信息?,JavaEE 6文檔只是提到@關鍵字。

丑陋,但這應該適合你:

<p:commandButton action="..." update=":#{component.namingContainer.parent.namingContainer.clientId}:form2:table2" />

由於您已經在使用PrimeFaces,另一種方法是使用#{p:component(componentId)} ,此幫助函數會掃描整個視圖根目錄以查找具有給定ID的組件,然后返回其客戶端ID:

<p:commandButton action="..." update=":#{p:component('table2')}" />

丑陋的回答很有效

update=":#{component.namingContainer.parent.namingContainer.clientId}:form2:table2

主要是從打開的對話框更新到父數據表更有用

除了上面的解決方案,我遇到了問題,我必須基於服務器端邏輯動態生成要更新的組件(很多)(可能更難找到嵌套)。

因此,服務器端的解決方案相當於update=":#{p:component('table2')}" 1 ,它使用org.primefaces.util.ComponentUtils.findComponentClientId( String designId )

// UiPnlSubId is an enum containing all the ids used within the webapp xhtml.
// It could easily be substituted by a string list or similar.
public static String getCompListSpaced( List< UiPnlSubId > compIds ) {

    if ( compIds == null || compIds.isEmpty() )
        return "" ;
    StringBuffer sb = new StringBuffer( ":" ) ;
    for ( UiPnlSubId cid : compIds )
        sb.append( ComponentUtils.findComponentClientId( cid.name() ) ).append( " " ) ;
    return sb.deleteCharAt( sb.length() - 1 ).toString() ;  // delete suffixed space
}

通過使用它的其他方法調用,例如... update="#{foo.getCompListComputed( 'triggeringCompId' )}"

1 :首先我試着沒有太多想法返回public static String getCompListSpaced0() { return ":#{p:component('table2')}" ; } public static String getCompListSpaced0() { return ":#{p:component('table2')}" ; }... update="#{foo.getCompListSpaced0()}表達,這當然沒有解決(想着怎么框架工程:)后)(返回為是),並用它可以引起問題的一些用戶此外,我的Eclipse / JBoss Tools環境建議編寫:#{p.component('table2')} (“。”而不是“:”)當然沒有幫助。

您可以使用binding屬性聲明綁定到JSF組件的EL變量。 然后,您可以使用javax.faces.component.UIComponent.getClientId()訪問此組件的絕對客戶端ID。 見下面的例子:

<t:selectOneRadio 
   id="yourId"
   layout="spread"
   value="#{yourBean.value}"
   binding="#{yourIdComponent}">
       <f:selectItems value="#{someBean.values}" />
</t:selectOneRadio>
<h:outputText>
   <t:radio for=":#{yourIdComponent.clientId}" index="0" />
</h:outputText>

試試這個:

<h:commandButton value="Click me">
    <f:ajax event="click" render="table" />
</h:commandButton>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM