0

how can I get a reference to a DIV (with a given id) from a jsf2 managed bean?

I've the following jsf template:

...
<h:body>
    <p:tabView>  
        <p:tab title="Test">  
            <div id="top" class="top">
                <ui:insert name="top"></ui:insert>
            </div>
            <div id="content" class="center_content">
                <ui:insert name="content"></ui:insert>
        </div>
        </p:tab>  
</h:body>
...

And this page:

<body>
    <ui:composition template="./pagesTemplate.xhtml">
        <ui:define name="top"> 
            ...
            <h:form id="schedulingSearchForm">
                ...
                <p:commandButton id="findButton" 
                                     value="Search"
                                     action="#{searchSchedulingBean.executeSearch()}"
                                     update="@form" 
                                     ajax="true"/>
            </h:form>
        </ui:define>
        <ui:define name="content">
        </ui:define>
    </ui:composition>
</body>

I would like that when the button with id "findButton" is pressed the managed bean "searchSchedulingBean" executes the method "executeSearch" that performs a search and displays the result in a primefaces data table that will be inserted in the "content" section of the page ( or in the div with id "content" declared in the template).

This is the method of my managed bean that is executed when the button is pressed (I leave pseudocode) . How can I get from there the reference to "content"?

public void executeSearch() {
    logger.info("Method executed");
    //There I get the data from the database.
    //But I don't know how get a reference to <ui:define name="content"> and create in it a new primefaces datatable to show that data.     
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user2572526
  • 1,219
  • 2
  • 17
  • 35
  • You can't. The div is not a JSF component. You can get a reference to the datatable if you use a component binding: http://stackoverflow.com/questions/12506679/what-is-component-binding-in-jsf-when-it-is-preferred-to-be-used – Gimby Jul 07 '14 at 14:07
  • Thanks for your reply. The problem is that the datatable doesn't exist before the button pressing, I would like to create it when the user press the button. I could create an hidden empty datatable and show it only when the button is pressed but I don't want to apply a workaround. What is the best practice in those cases? I suppose this is a very common situation. – user2572526 Jul 07 '14 at 18:38
  • 1
    Well you could wrap the datatable in a h:panelGroup (which can also render as a div or a span by the way based on what layout you give it) and then use ajax re-rendering to rerender the h:panelGroup when you click the button; inside it the datatable conditionally renders using the rendered property, so you can have full control over when it renders and when it doesn't. That's how I would do it but there are so many ways one can learn by reading a good book on JSF. – Gimby Jul 07 '14 at 21:51

0 Answers0