【发布时间】:2015-10-16 12:53:31
【问题描述】:
我有具有 4 到 5 个输入组件的 primefaces 数据表。输入之一是 p:autocomplete。有一个通过 UI 添加新行的选项,我稍后添加了第一行,我尝试添加第二行,无论第一行中选择的值在第二行中保持不变。除此之外,如果我通过自动完成在第二个中选择不同的值,则该值反映在两行中。
但我想保留为第一行选择的值,然后我需要以不同的方式保留第二行值。
注意:我使用 p:selectonemenu 来显示数据库中的值,它有 10000 多条记录,需要更多时间。由于性能问题,我选择了 p:autocomplete
请看代码 XHTML 代码 - LocationDetails.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:t="http://myfaces.apache.org/tomahawk"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui">
<h:form id="atcs">
<p:dataTable var="atcscirreq" id="atcscirreqtbl"
value="#{atcsCircuitIdAddressRequestBean.atcsAddressCircuitRequestList}"
binding="#{atcsCircuitIdAddressRequestBean.dataTable}" lazy="true" resizableColumns="true">
<p:column id="locn">
<p:autoComplete id="lcnLst" required="true"
requiredMessage="LocationName is required field"
converter="locationNameAutoCompleteConverter"
completeMethod="#{atcsCircuitIdAddressRequestBean.locationNames}"
var="locn" itemLabel="#{locn.locName}" itemValue="#{locn}"
value="#{atcsCircuitIdAddressRequestBean.locnInfo}"
emptyMessage="No Records Found" maxResults="10"
forceSelection="true">
</p:autoComplete>
</p:column>
</p:dataTable>
<p:commandButton value="Add Another Request" action="# {atcsCircuitIdAddressRequestBean.addNewRequestData}" update="atcscirreqtbl">
</h:form>
ATCSCircuitIdAddressRequestBean.java
package com.bean.request;
public class ATCSCircuitIdAddressRequestBean {
TblTrackLocationinformation locnInfo = new TblTrackLocationinformation();
List<TblTrackLocationinformation> filteredLocations = new ArrayList<TblTrackLocationinformation>();
private List<TblTrackLocationinformation> atcsAddressCircuitRequestList = new ArrayList<TblTrackLocationinformation>();
public List<TblTrackLocationinformation> locationNames(String name) {
List<TblTrackLocationinformation> allLocations = service.getAllLocations;
if (name.trim().equals(""))
return allLocations;
for (int i = 0; i < allLocations.size(); i++) {
TblTrackLocationinformation data = allLocations.get(i);
if (data.getLocName().toString()
.contains(name)
|| data.getLocName().toLowerCase()
.contains(name.toLowerCase())) {
filteredLocations.add(data);
}
}
return filteredLocations;
}
public TblTrackLocationinformation getLocnInfo() {
return locnInfo;
}
public void setLocnInfo(TblTrackLocationinformation locnInfo) {
this.locnInfo = locnInfo;
}
public void addNewRequestData() {
TblTrackLocationInformationdata = new TblTrackLocationInformation();
filteredLocations=new ArrayList<TblTrackLocationinformation>();
atcsAddressCircuitRequestList.add(data);
}
}
TblTrackLocationInformation.java
package com.bean.request;
public class TblTrackLocationInformation{
private String locName;
private Integer locationId;
public String getLocName() {
return locName;
}
public void setLocName(String locName) {
this.locName = locName;
}
public Integer getLocationId() {
return locationId;
}
public void setLocationId(Integer locationId) {
this.locationId = locationId;
}
}
LocationNameAutoCompleteConverter.java
package com.bean.request;
@FacesConverter("locationNameAutoCompleteConverter")
public class LocationNameAutoCompleteConverter implements Converter{
LocationInfoDAO locnDao = new LocationInfoDAOImpl();
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
return locnDao.getLocationInfoById(Integer.valueOf(value));
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
return String.valueOf(((TblTrackLocationinformation) value)
.getLocationId());
}
}
项目规格为JSF 2.1、Primefaces 5.0、Servlet 2.5
我已经搜索了很多链接,但我没有得到答案。我正在检查过去 2 天。 请帮帮我!
【问题讨论】:
-
@Kukeltje 谢谢。我已经添加了所需的信息。如果您需要更多信息来帮助我,请告诉我。
-
@BalusC,谢谢。我已经根据 stackoverflow 标签进行了更新。请帮帮我
-
JSF 2.1 是 api 'spec' 版本,而不是实现版本(例如 mojarra 2.1.22)
-
而且它不是minimal reproducible example...您是否尝试将这段代码按原样发布在一个干净的新jsf项目中并运行它?
标签: jsf-2 primefaces autocomplete