18

I have a template and in its Definition I use several forms and buttons.

The problem is the definition (define) xhtml file does not know the component hierarchy.

And for example I want to update the element "table2" in a different form in the same define file.

Template Insert:

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

defines the first level of my hierarchy "nav"

Template define:

<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>

In my define part I don't want to know "nav"!

How can I do this? or how can I move one naming component upwards?, or save the highest parent complete id in a variable?

sometimes i saw something like:

update=":table2"

But I could not find any informations about this?, the JavaEE 6 documentation just mentions the @ keywords.

djmj
  • 5,579
  • 5
  • 54
  • 92

5 Answers5

52

Ugly, but this should work out for you:

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

As you're already using PrimeFaces, an alternative is to use #{p:component(componentId)}, this helper function scans the entire view root for a component with the given ID and then returns its client ID:

<p:commandButton action="..." update=":#{p:component('table2')}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Thanks for your answer. #{component.namingContainer.parent.namingContainer.clientId} works fine. But I hoped for a more elegant solution (syntax sugar). Similar to "../" for folderstructures. Why did you still wrote a colon as a prefix? I am new to jsf so I ask. And I do not think its that ugly since using the unknown naming container clientId directly is more ugly since I could use that template multiple times. I think I will define myself a EL function to get the clientId of a sibling naming container. – djmj Jan 16 '12 at 01:35
  • 1
    The colon is the default `NamingContainer` separator character (which is overrideable by a context param though). When using it as prefix, it will be interpreted as "absolute" client ID, i.e. it's been resolved relative to the `UIViewRoot` instead of the current `NamingContainer` (which can be the composite, or datatable, or form, etc). As to the nicer solution, use `p:component()` function instead (or homebrew one yourself). – BalusC Jan 16 '12 at 02:21
  • 3
    p:component doesnt work for me. I'm really sad with this JSF behavior. Nothing works. – MaikoID Apr 24 '13 at 20:42
  • 6
    @MaikoID: Just press `Ask Question` with a decent [SSCCE](http://stackoverflow.com/tags/jsf/info) if you need help instead of sitting back doing nothing else than complaining and crying because you can't debug it yourself. – BalusC Apr 24 '13 at 20:45
  • Great advice! It helped me with updating only specified component within an active tab in PrimeFaces TabView. – iozee Dec 20 '14 at 11:47
  • @BalusC does the same logic hold for `datasource` attribute of `p:droppable` ? I am struggling to map the correct datasource. Exact problem: https://stackoverflow.com/questions/46551952/datasource-mapping-when-draggable-and-droppable-in-different-naming-containers-a – Himanshu Arora Oct 03 '17 at 21:00
  • @BalusC, once again a solution of yours saved my life: :#{p:component('..')} thingy is perfect. Your posts are a blessing! Thanks, mate! – Hugo Tavares Aug 02 '18 at 11:13
1

ugly answer works well

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

mainly more useful updating from opened dialog to parent datatable

kolossus
  • 20,559
  • 3
  • 52
  • 104
1

You may use binding attribute to declare EL variable bound to JSF component. Then you may access absolute client id of this component by using javax.faces.component.UIComponent.getClientId(). See example below:

<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>
Dmitry Trifonov
  • 1,079
  • 11
  • 13
0

Additionally to the solutions above I had the problem, that I had to dynamically generate the to-be-updated components (many) based on server-side logic (with maybe harder to find out nesting).

So the solution on the server-side is an equivalent to update=":#{p:component('table2')}"1 which uses 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
}

called via some other method using it, e.g. like ... update="#{foo.getCompListComputed( 'triggeringCompId' )}".

1: first I tried without too much thinking to return public static String getCompListSpaced0() { return ":#{p:component('table2')}" ; } in an ... update="#{foo.getCompListSpaced0()} expression, which of course (after thinking about how the framework works :) ) is not resolved (returned as is) and may cause the issues with it some users experienced. Also my Eclipse / JBoss Tools environment suggested to write :#{p.component('table2')} ("." instead of ":") which did not help - of course.

Andreas Covidiot
  • 4,286
  • 5
  • 51
  • 96
0

Try this:

<h:commandButton value="Click me">
    <f:ajax event="click" render="table" />
</h:commandButton>
Ventsislav Marinov
  • 594
  • 1
  • 6
  • 14
  • I wasn't precise enough. I want to update a particular element by its id, since not all elements are in the same form. I edited my post to make it clear. What does the @table do precisely? If both elements are siblings I could just use render="table" without @? – djmj Jan 13 '12 at 08:07
  • You could to use render="table". – Ventsislav Marinov Jan 13 '12 at 08:47
  • 1
    What is render="table"...? – Andrew Apr 13 '17 at 15:57