primefaces p:使用数据表自动填充下一行中重复的选定值

时间:2015-10-16 12:53:32

标签: jsf-2 primefaces autocomplete

我有primefaces数据表,有4到5个输入组件。其中一个输入是p:autocomplete。有一个选项可以通过UI添加新行,我已经添加了第一行,我试图添加第二行,在第一行中选择的任何值,它在第二行保持相同。除此之外,如果我在第二次到自动完成中选择不同的值,则该值反映两行。

但我想保留为第一行选择的值,然后我需要以不同方式保留第二行值。

注意:我使用了p:selectonemenu来显示来自DB的值,它有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天。 请帮帮我!

1 个答案:

答案 0 :(得分:0)

很长一段时间后,我开始了解解决方案。我们需要知道何时以及如何使用List with object和List with String。 必须使用

public List<String> locationNames(String name)

而不是

public List<TblTrackLocationinformation> locationNames(String name)