2012-01-26 17 views
1

氷の中に文字を入力するときにオートコンプリートをしようとしています: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; 
     } 

答えて

1

。リストをオーダーするのではなく、リストをフィルタリングする(またはマッチだけを含む新しいリストを作成する)必要があります。次にオートコンプリートリストがレンダリングされると、バインドされたリストが再び評価されます。

アイスフェースのチュートリアルには、いくつかのソースが添付されています(下)。 AutoCompleteBeanを見てください。メソッドupdateList(ValueChangeEvent e)setMatches(e)となります。この方法では、リストに新しいものが割り当てられます。

// assign new matchList 
if (this.matchesList != null) { 
    this.matchesList.clear(); 
    this.matchesList = null; 
} 
this.matchesList = matchList; 

これにより、入力に一致する項目のみが表示されます。

合計すると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())); 
     } 
    } 
} 
+0

私は私はすでにこのコードのビットを使用しています、これに疑問を持っていますupdatelist。 '//新しいmatchListを割り当てます if(this.matchesList!= null){ this.matchesList.clear(); this.matchesList = null; } this.matchesList = matchList; 'なぜリストに含まれる項目を減らすことができないのですか?私はあなたのポイントを理解していなかったので、この質問をして申し訳ありません。 – user75ponic

+1

@Polappanこんにちは。私はまた、このコンポーネントを使用します。ユーザーが何かを入力するたびに、結果を返すLucene検索を開始します。検索が開始されるたびに、SelectItemの新しいリストリストを作成する必要があります。この一時的なリストは、コンポーネントにバインドされているBeanのメンバーに割り当てられます。 この時点で、あなたのリストのアイテムをどのように減らすのかを理解することは難しいです。上のコードは並べ替えのみを示しています。削減(および再割り当て)がどのように行われたかを示すいくつかのコードを提供できますか? –

+0

ご協力いただきありがとうございます。上記の元の投稿に更新コードを追加しました。私のコードはデータソース以外のチュートリアルコードとほとんど同じです。 – user75ponic