Primefaces 自动完成保留选定的下拉值

Primefaces autocomplete keep selected dropdown value

我有一个 Primefaces 自动完成框,并且需要将下拉列表的值保持在 selected 值,例如我有一个包含 20 个元素的列表,我 select 第 10 个元素。

如果我想 select 另一个值,当我 select 下拉菜单 它从列表的开头开始 :

这是我的 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>

我在保存值时没有问题,只是在视图中。

¿有没有办法在 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 to get the current component。它将为您提供 p:autoComplete 组件,您可以从中读取值。

如果您不会使用 OmniFaces,请阅读 How to find out the calling component in the getter method?