0

So, I have a dataTable that looks like this:

                <h:form>
        <h:dataTable value="#{backingBean.employeeLineItems}" var="emp">
            <h:column>
                <f:facet name="header">First</f:facet>
                #{emp.lastname}
            </h:column>
            <h:column>              
                <f:facet name="header">Last</f:facet>
                #{emp.firstname}
            </h:column>
            <h:column>
                <f:facet name="header">Actions</f:facet>
                <h:commandButton value="View Details"> 
                    <f:ajax execute="#{setCurrentEmployeeId(emp.id)}" render="employeeDetails"/> 
                </h:commandButton>
            </h:column>
        </h:dataTable>
                    <h:outputText value="#{backingBean.employeeDetails}" id="employeeDetails"/>
                </h:form>

For each row of the datatable, there is a button that I want to, when clicked, ajax the employeeLineItem id value over to a method that sets that id in the backing bean, and then renders the outputText tag with id "employeeDetails" (The getEmployeeDetails method would use the employeeLineItem id to get the right employee details object from the database, of course)

My solution doesn't seem to be working, does anyone know what I'm doing wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
bitsmcgee77
  • 391
  • 3
  • 10

1 Answers1

2
<h:commandButton value="View Details"> 
   <f:ajax execute="#{setCurrentEmployeeId(emp.id)}" render="employeeDetails"/> 
</h:commandButton>

This is wrong. The execute attribute of <f:ajax> should point to a space separated collection of component client IDs which are to be submitted and processed in the server side (the same way as you specify the render attribute with a space separated collection of component client IDs which are to be updated/re-rendered after the ajax request). In your particular case, it should have been the ID of the datatable or the form, or just @form to generically refer the parent form.

The action method wherein you pass the row ID should be definied in the action attribute of the <h:commandButton> instead. So, this should do:

<h:commandButton value="View Details" action="#{backingBean.setCurrentEmployeeId(emp.id)}">
    <f:ajax execute="@form" render="employeeDetails"/> 
</h:commandButton>

(note that I fixed the missing managed bean name in the action method)

By the way, are you aware that you can also just pass whole objects along as arguments in EL?

<h:commandButton value="View Details" action="#{backingBean.setCurrentEmployee(emp)}">
    <f:ajax execute="@form" render="employeeDetails"/> 
</h:commandButton>

This way you don't need to reload the employee from the DB.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, I fixed my code to use your approach. However, using causes an error " contains an unknown id 'employeeDetails' - cannot locate it in the context of the component j_idt21" . It seems the outputText tag can't be resolved, even though I am referrring to it by id, and it's in the same form?? – bitsmcgee77 Oct 31 '11 at 18:53
  • The `` is also an `NamingContainer` component. You'd need to specify the absolute ID (give your `` an ID and use `render=":formId:employeeDetails"` instead. Or bind the output component to the view and use `render=":#{outputComponent.clientId}"`. – BalusC Oct 31 '11 at 18:55
  • Done. By the way, any idea why the ajax call to render the outputText element would insist on trying to call my datatable collection getter in the backing bean? My getEmployeeDetails method makes absolutely no reference to it. I had assumed that the rendering simply makes changes to the outputText method inline, everything else on the page being unaffected. – bitsmcgee77 Oct 31 '11 at 19:58
  • The getter is called during apply request values phase because JSF needs to identify the commandbutton which has been pressed in order to execute the action and pass the currently iterated row object along it. See also http://stackoverflow.com/q/2118656 and http://stackoverflow.com/q/2090033 – BalusC Oct 31 '11 at 20:00