我有一个Primefaces 自动完成框,并且需要将下拉列表的值保留在所选值上,例如我有一个包含 20 个元素的列表我选择 10 元素。
如果我想在选择下拉列表时选择另一个值它从列表的开头开始:
这是我的 primefaces 代码:
<p:autoComplete id="transportLAvailable" dropdown="true"
value="#{remissionOrderReportController.selectedTLineFilter}"
forceSelection="true" requiredMessage="Some message"
completeMethod="#{remissionOrderReportController.searchFromTLinesList}"
var="transportFiltered" itemLabel="#{transportFiltered.name}"
itemValue="#{transportFiltered}" converter="#{transportLineConverter}">
<p:ajax event="itemSelect" process="@this"
listener="#{remissionOrderReportController.findVehicleByTL(transportFiltered)}"/>
</p:autoComplete>
我在保存值时没有遇到问题,只是在 View 方面遇到问题。
有没有办法在 p:autoComplete 或 javascript 的属性上执行此操作?
最佳答案
诀窍是简单地将当前值包含在返回列表的顶部。我这样做是这样的:
public List<T> autoCompleteItems(final String query) {
List<T> results = ...; // Find results for the query
addBoundValue(Components.getCurrentComponent(), results);
return results;
}
protected void addBoundValue(UIInput input, List<T> results) {
if (input.getValue() != null && typeTclass.isAssignableFrom(input.getValue().getClass())) {
T bound = typeTclass.cast(input.getValue());
results.remove(bound); // If it's already in the list, remove it first
results.add(0, bound); // Add the value at the top of the list
}
}
此代码来自一个通用 bean,它使用列表的类型作为参数(类型在构造函数中设置为 typeTclass
)。
请注意,我正在使用 OmniFaces获取current component 。它将为您提供 p:autoComplete
组件,您可以从中读取值。
如果您无法使用 OmniFaces,请阅读 How to find out the calling component in the getter method?
关于javascript - Primefaces 自动完成功能保留选定的下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56369088/