Hy What i am trying to do si to create an autocomplete input text ,but when the form is submited i want to set an id or an employee Object, not to obtain the string from the input field.can someone help ..how can i use a hiddeninput field or an javascript / ajax function to do that.THX ..
<rich:autocomplete mode="cachedAjax" tokens="," minChars="0"
autoFill="true" selectFirst="true"
autocompleteMethod="#{employeeBean.employeeSuggestions}" var="employee"
fetchValue="#{employee.firstName} #{employee.lastName}">
<h:inputHidden id="employeeId" value="#{employee.id}"/>
<h:column >
<h:outputText value="#{employee.firstName}" />
<h:outputText value=" " />
</h:column>
<h:column>
<h:outputText value="#{employee.lastName}" />
</h:column>
</rich:autocomplete>
</h:panelGrid>
<h:panelGrid columns="3" cellspacing="5">
<h:commandButton value="#{messages.ok}"
action="#{departmentBean.addOrUpdateDepartment}"&g开发者_开发知识库t;
</h:commandButton>
<h:commandButton action="department" value="#{messages.close}"
immediate="true" />
</h:panelGrid>
i`m open to any suggestions.
I've got the same situation. The ultimate answer that I worked out is to use a jsFunction, combined with an action method in the backing bean.
First, the ID value will be fetched into the autocomplete field for a brief instant. The onselectitem JS function is called. The execute attribute of the jsFunction ensures that the fetched ID value is bound into the backing bean field before my populateEmployee action is called. populateEmployee turns around, moves the ID value in the field into wherever I want it, and replaces the value of the autocomplete field with whatever I ultimately want displayed (in this case, the name). Then the name autocomplete field is re-rendered.
Depending on how fast your backend system is with the employee ID-to-name lookup, the underlying ID will be displayed for a brief moment, while your equivalent of populateEmployee runs. But this is as close as I can get to an acceptable (in my case) working solution.
JSF:
<a4j:jsFunction name="chooseEmployee" execute="employeeSearchName" action="#{employeeSearchBean.populateEmployee}" render="selectedEmployeeId employeeSearchName"/>
<rich:autocomplete id="employeeSearchName" mode="ajax" minChars="3"
autocompleteMethod="#{employeeSearchBean.searchEmployees}" var="emp"
autofill="false" layout="table"
fetchValue="#{emp.employee.id}" value="#{employeeSearchBean.selectedEmployeeName}"
onselectitem="chooseEmployee()">
... output columns ...
</rich:autocomplete>
<h:outputText id="selectedEmployeeId" value="#{employeeSearchBean.selectedEmployeeId}"/>
Backing Bean:
private String selectedEmployeeName;
private Integer selectedEmployeeId;
public void populateEmployee() {
selectedEmployeeId = Integer.parseInt(selectedEmployeeName);
for (EmployeeSearchEntry entry : data) {
if (entry.getEmployee().getId().equals(selectedEmployeeId)) {
selectedEmployeeName = entry.getNameLfm();
break;
}
}
}
You can use a4j:param
within the JS function and assign param value to asignedTo Value.
And use event onselectitem="chooseEmployee(this.value)"
in rich:autocomplete
精彩评论