2010-06-14 36 views
5

我需要創建一個SuggestBox,它將顯示按下Enter鍵 上的所有選項。 我寫了下面的實現,它似乎是 工作正常。 我希望有人審查我的實施,並讓我知道,如果它 將導致任何特定情況下的問題。 此外,通過調用MultiWordSuggestOracle上的方法 setDefaultSuggestions(),傳遞給此SuggestBox的SuggestOracle應該有 默認建議集。我的 SuggestBox的任何用戶都應該對這個事實透明。因此,我想我會 需要包裝(或擴展)MultiWordSuggestOracle做默認 建議設置。你能推薦一下這樣做的好方法 嗎?SuggestBox GWT顯示輸入鍵上的所有選項

public class SuggestBoxWithAllOptions extends SuggestBox implements 
    KeyPressHandler { 
    public SuggestBoxWithAllOptions(MultiWordSuggestOracle oracle) { 
      super(oracle); 
      this.addKeyPressHandler(this); 
    } 
    @Override 
    public void onKeyPress(KeyPressEvent event) { 
      char c = event.getCharCode(); 
      int i = this.getText().length(); 
    if (c == KeyboardListener.KEY_ENTER && i == 0) { 
      /* Since the query string is null, the default suggestions 
      will get listed */ 
      this.showSuggestionList(); 
    } 
    } 
    } 

    /* Code for initializing the SuggestBox */ 
      List<String> suggestions = new ArrayList<String>(); 
      suggestions.add("Tablet"); 
      suggestions.add("Capsule"); 
      MultiWordSuggestOracle myOracle = new MultiWordSuggestOracle(); 
      myOracle.addAll(suggestions); 
      myOracle.setDefaultSuggestionsFromText(suggestions); 
      SuggestBox mySuggest = new SuggestBoxWithAllOptions(myOracle); 

回答

5

這對我來說很不錯。另一種方法是添加一個按鈕來顯示所有建議。按鈕可以被設計成看起來像一個下拉框箭頭。

public class DropDownSuggestBox extends Composite { 

public DropDownSuggestBox(final SuggestBox suggestBox) { 
    FlowPanel layout = new FlowPanel(); 
    final Button dropDownButton = new Button(); 
    dropDownButton.setStyleName("slate-DropdownIcon"); 
    dropDownButton.setText("Show Options"); 
    dropDownButton.addClickHandler(new ClickHandler() { 
     @Override 
     public void onClick(ClickEvent event) { 
      suggestBox.setFocus(true); 
      suggestBox.showSuggestionList(); 
     } 
    }); 

    layout.add(suggestBox); 
    layout.add(dropDownButton); 

    initWidget(layout); 

    setStylePrimaryName("slate-DropDownSuggestBox"); 
} 

} 
2

這種做法是有問題的,因爲重點提示框消失,當你按一下按鈕,所以誰觸發模糊驗證等人,可以用這種行爲來干擾。

我建議使用此LIB這是一個現實生活中的巨大應用程序創建的,所以很多practicle例使用 http://code.google.com/p/advanced-suggest-select-box/

後,解決了因反饋對於第一個原來的問題,你可以覆蓋的onkeyup()和有爲你的情況做特殊的事情,並委託super.onKeyUp()爲其餘所有。

希望它可以幫助, 最好的問候, Zied哈姆迪

0

@Vishal辛格,

無需延長SuggestBox。 您的代碼適用於基本的SuggestBox。

SuggestBox mySuggest = new SuggestBox(myOracle); 
    mySuggest.addKeyPressHandler(new KeyPressHandler() { 
     @Override 
     public void onKeyPress(KeyPressEvent event) { 

     } 
    });