当我在ice中输入字符时,我正在尝试自动完成:selectInputText 我面临的问题是,当我输入字符时,它会带来与我输入的字符不匹配的名称。 请参阅以下屏幕截图以供参考。
a http://s7.postimage.org/46jk4tka3/autocomplete.png
理想情况下,自动填充应仅显示结果中的第一行,但是它会显示与我键入的字符不匹配的行。
仅显示Abell Maryland 20606。
这是用于比较的代码,如何修改它以满足我的要求,即只显示与我输入的匹配的结果。
public int compare(Object o1, Object o2) {
if (o1 instanceof SelectItem) {
s1 = ((SelectItem) o1).getLabel();
} else {
s1 = o1.toString();
}
if (o2 instanceof SelectItem) {
s2 = ((SelectItem) o2).getLabel();
} else {
s2 = o2.toString();
}
return s1.compareToIgnoreCase(s2);
}
};
我正在关注Icefaces的这个教程
http://wiki.icefaces.org/display/ICE/Auto-Complete
更新
我在autocomplete.jspx中的代码
<ice:selectInputText rows="10" width="300"
listVar="emp"
valueChangeListener="#{mybean.updateList}"
listValue="#{mybean.list}">
<f:facet name="selectInputText">
<ice:panelGrid columns="3" columnClasses="empNameCol">
<ice:outputText value="#{emp.empName}"/>
</ice:panelGrid>
方法updateList
public void updateList(ValueChangeEvent event) {
setMatches(event);
if (event.getComponent() instanceof SelectInputText) {
SelectInputText autoComplete = (SelectInputText)event.getComponent();
if (autoComplete.getSelectedItem() != null) {
bean = (Bean)autoComplete.getSelectedItem().getValue();
}
else {
Bean tempCity = getMatch(autoComplete.getValue().toString());
if (tempCity != null) {
bean = tempCity;
}
}
}
}
方法setMatches
private void setMatches(ValueChangeEvent event) {
Object searchWord = event.getNewValue();
int maxMatches = ((SelectInputText)event.getComponent()).getRows();
List matchList = new ArrayList(maxMatches);
try {
int insert =
Collections.binarySearch(dictionary, searchWord, AutoCompleteDictionary.LABEL_COMPARATOR);
if (insert < 0) {
insert = Math.abs(insert) - 1;
}
for (int i = 0; i < maxMatches; i++) {
if ((insert + i) >= dictionary.size() || i >= maxMatches) {
break;
}
matchList.add(dictionary.get(insert + i));
}
} catch (Throwable e) {
e.printStackTrace();
logger.error("Erorr finding autocomplete matches" + e.getMessage());
}
if (this.matchesList != null) {
this.matchesList.clear();
this.matchesList = null;
}
this.matchesList = matchList;
}
更新2
修改后的setMatches方法
private void setMatches(ValueChangeEvent event) {
Object searchWord = event.getNewValue();
int maxMatches = ((SelectInputText) event.getComponent()).getRows();
List matchList = new ArrayList(maxMatches);
try {
for(int i = 0; i < dictionary.size(); i++) {
SelectItem s = (SelectItem)dictionary.get(i);
if(s.getLabel().startsWith(searchWord.toString())) {
matchList.add(s);
if(matchList.size() == maxMatches)
break;
}
}
} catch (Throwable e) {
e.printStackTrace();
logger.error("Erorr finding autocomplete matches" + e.getMessage());
}
if (this.matchesList != null) {
this.matchesList.clear();
this.matchesList = null;
}
this.matchesList = matchList;
}
答案 0 :(得分:1)
您必须更新SelectItems列表。您只需过滤列表(或创建仅包含匹配项的新列表),而不仅仅是对列表进行操作。下次自动完成列表呈现时,它将再次评估绑定列表。
icefaces教程附有一些资料(下图)。看看AutoCompleteBean
。方法updateList(ValueChangeEvent e)
调用setMatches(e)
。在此方法中,列表将分配一个新列表。
// assign new matchList
if (this.matchesList != null) {
this.matchesList.clear();
this.matchesList = null;
}
this.matchesList = matchList;
这会导致ui组件仅显示与输入匹配的项目。
总结:ice:selectInputList
将始终显示其列表中包含的项目,因此请减少列表中的项目以仅显示相关项目。
此致
更新
private void setMatches(ValueChangeEvent event) {
Object searchWord = event.getNewValue();
int maxMatches = ((SelectInputText)event.getComponent()).getRows();
List matchList = new ArrayList(maxMatches);
try {
for(int i = 0; i < dictionary.size(); i++) {
SelectItem s = dictionary.get(i);
if(s.getLabel().startsWith(searchWord)) {
matchList.add(s);
if(matchList.size() == maxMatches)
break;
}
}
} catch (Throwable e) {
e.printStackTrace();
logger.error("Erorr finding autocomplete matches" + e.getMessage());
}
if (this.matchesList != null) {
this.matchesList.clear();
this.matchesList = null;
}
this.matchesList = matchList;
}
// note: not optimized, just to explain how to do.
更新2(简短版)
/**
* Fills the suggestionList with the given luceneResult.
*
* @param suggestionList The list to fill.
* @param luceneResult The previously computed luceneResult.
*/
private static void fillLookupSuggestionList(final List<SelectItem> suggestionList,
LuceneResult luceneResult)
{
suggestionList.clear();
String searchQuery = luceneResult.getLuceneResultConfig().getSearchQuery();
if (luceneResult.getResultSize() <= 0)
{
suggestionList.add(new SelectItem(null, BundleHelper.i18n(LuceneLookupController.BUNDLE,
LuceneLookupController.NO_ITEM_FOUND)));
}
else
{
List<LuceneResultEntry> results = luceneResult.getResult();
for (LuceneResultEntry entry : results)
{
suggestionList.add(new SelectItem(entry.getMetaInfo(),
entry.getInfo().getDescription()));
}
}
}