2

I have an Employee object I am showing in inputtext. For example, the firstname of the employee is shown in an inputtext. When the value of this firstname changes it calls a method. Before this is done I want to call a method which saves the ID of the employee in the managedbean so I know which employee needs to be changed. How do I do this, I got this so far:

<h:outputText value="First name:"/>
<p:inplace id="firstname" editor="true">
     <p:ajax event="save" onsuccess="#{employeeController.saveName()}"/>
     <p:inputText id="firstName" value="#{emp.firstName}"  
                  required="true" label="text"
                  valueChangeListener="#{employeeController.firstNameChanged}">
     <p:ajax event="valueChange" listener="#{employeeController.onValueChangedStart}"/>
     </p:inputText>
</p:inplace>

I guess I should pass the ID with the onValueChangedStart or firstNameChanged method. How do I do this? Or is there a better way to do this? There is a getter for the emp. So #{emp}.id to get it.

Klaasvaak
  • 5,634
  • 15
  • 45
  • 68

3 Answers3

11

Assuming that you're indeed inside a repeating component where #{emp} is been exposed by its var attribute, you could just grab it from the EL scope inside the value change listener as follows:

FacesContext context = FacesContext.getCurrentInstance();
Employee employee = context.getApplication().evaluateExpressionGet(context, "#{emp}", Employee.class);
Long id = employee.getId();
// ...

Alternatively, if you wrap the value of the <h:dataTable> inside a DataModel<Employee>, then you can obtain the current row as follows:

Employee employee = model.getRowData();
Long id = employee.getId();
// ...

Unrelated to the concrete problem, it's unnecessary to have two listener methods for the value change. Note that the valueChangeListener gives you the opportunity to get both the old and new value by the event and the p:ajax listener not, the new value is already been set as model value at that point. If you need both the old and new value, remove the listener attribute of <p:ajax>. The event="valueChange" is by the way the default event already, so just remove it as well.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Do you know if it's also possible to render the inplace again? It still shows the old value. – Klaasvaak Mar 21 '12 at 14:04
  • This doesn't seem to work, I put a javascript with alert to test if it renders, it does. But it looks like it renders it before it does the value change. I use When I refresh the page it does have the correct value. – Klaasvaak Mar 21 '12 at 14:25
  • Please ask a new question. It's unrelated to the initial question. – BalusC Mar 21 '12 at 14:27
  • Got it solved adding a p:ajax event="save" update="@this". Added this beneath the other p:ajax line. – Klaasvaak Mar 21 '12 at 15:08
  • I have adapted this suggestion by BalusC to use an "elegant hack" to exploit the id of the p:inplace to propagate (to a save event listener) a translated form of the EL expression for an entity whose value is being edited by inplace: http://stackoverflow.com/questions/11405613/jsf-primefaces-pinplace-how-to-more-elegantly-propagate-the-el-expression-for – Webel IT Australia - upvoter Jul 10 '12 at 01:57
  • Imagine it inside ui:repeat, and we want to get row data in listener. But UIRepeat has private access to getDataModel(). Is there a way to get row data without evaluationg expression "#{emp}"? – antonu17 Jan 12 '16 at 05:08
5

Using primefaces ajax you can retrieve the value of an input in java doing:

public void onValueChanged(AjaxBehaviorEvent event)
{
    Employee employee = (Employee)((UIOutput)event.getSource()).getValue();
    //...
}

<h:selectOneMenu required="true"
    value="#{empl}" converter="#{bean.employeeConverter}">
    <f:selectItems value="#{bean.employees}" var="varEmployee" 
        itemLabel="#{varEmployee}" itemValue="#{varEmployee}"/>
    <p:ajax event="change" listener="#{bean.onValueChanged}"/>
</h:selectOneMenu>

Instead of casting to employee you cast to String since your value is a String, or use the employee Object and use a Converter or its toString() method.

djmj
  • 5,579
  • 5
  • 54
  • 92
2

try

<p:ajax event="save" onsuccess="#{employeeController.saveName(emp.id)}"/>
Kragh
  • 403
  • 4
  • 11