2013-03-14 78 views
1

我有一個文本框用於在我的應用程序中搜索產品。當用戶在文本框中輸入內容時,應該顯示建議,以便用戶可以搜索任何特定的產品類別。GWT意見箱覆蓋默認行爲

例如,如果產品被歸類爲男裝,女裝,童裝,夏季,冬季和用戶輸入的「磨腳」,建議列表應該顯示如下:

foot wear in Men 
foot wear in Women 
foot wear in Kids 
foot wear in Summer 
foot wear in Winter 

上KeyUp事件,我清除當前的建議,並通過附加每個類別名稱與文本中輸入的用戶來填充新的建議列表。它可以正常工作,但用戶不會輸入與類別名稱相匹配的任何內容。也就是說,如果用戶輸入「贏」,建議只顯示

Wi in Winter 

但我預計的建議是

Win in Men 
Win in Women 
Win in Kids 
Win in Summer 
Win in Winter 

誰能告訴什麼可能是錯誤的。

謝謝。

更新 - 附代碼

其實我創建了一個自定義的widget。下面是我的Java和XML模板

public class MySearchBox extends Composite implements IsWidget, HasKeyDownHandlers, HasSelectionHandlers<Suggestion>, KeyUpHandler, ClickHandler, BlurHandler 
{ 

private static MySearchBoxUiBinder uiBinder = GWT.create(MySearchBoxUiBinder.class); 

interface MySearchBoxUiBinder extends UiBinder<Widget, MySearchBox> 
{ 
} 

private static final List<String> PDT_CHANNELS = Arrays.asList("Men", "Women", "Kids", "Summer", "Winter"); 

@UiField 
SuggestBox mySuggestionBox; 

public MySearchBox() 
{ 
    initWidget(uiBinder.createAndBindUi(this)); 
    initialize(); 
} 

private void initialize() 
{ 

    mySuggestionBox.getTextBox().addKeyUpHandler(this); 
    mySuggestionBox.getTextBox().addClickHandler(this); 
    mySuggestionBox.getTextBox().addBlurHandler(this); 

} 

@Override 
public void onKeyUp(final KeyUpEvent event) 
{ 
    if(event.getNativeKeyCode() != KeyCodes.KEY_ENTER) 
    { 
     populateOracle(mySuggestionBox.getText().trim()); 
    } 
} 

private void populateOracle(final String inputText) 
{ 

    String searchText = ""; 
    if(inputText != null) 
    { 
     searchText = inputText.trim(); 
    } 
    if((searchText == null || searchText.length() < 1)) 
    { 
     clearOracle(); 
     mySuggestionBox.showSuggestionList(); 
     return; 
    } 
    final List<String> oracleSuggestions = new ArrayList<String>(); 
    for(String scope : PDT_CHANNELS) 
    { 
     oracleSuggestions.add(searchText + " in " + scope); 
    } 
    populateOracle(oracleSuggestions); 

} 

private void clearOracle() 
{ 
    final MultiWordSuggestOracle oracle = (MultiWordSuggestOracle)mySuggestionBox.getSuggestOracle(); 
    final DefaultSuggestionDisplay suggestionDisplay = (DefaultSuggestionDisplay)mySuggestionBox 
      .getSuggestionDisplay(); 
    if(suggestionDisplay.isSuggestionListShowing()) 
    { 
     suggestionDisplay.hideSuggestions(); 
    } 
    oracle.clear(); 
} 

private void populateOracle(final List<String> data) 
{ 
    final MultiWordSuggestOracle oracle = (MultiWordSuggestOracle)mySuggestionBox.getSuggestOracle(); 
    oracle.clear(); 
    oracle.addAll(data); 
    final DefaultSuggestionDisplay suggestionDisplay = (DefaultSuggestionDisplay)mySuggestionBox 
      .getSuggestionDisplay(); 
    if(!suggestionDisplay.isSuggestionListShowing()) 
    { 
     mySuggestionBox.showSuggestionList(); 
    } 
} 

@Override 
public HandlerRegistration addSelectionHandler(final SelectionHandler<Suggestion> handler) 
{ 
    return mySuggestionBox.addHandler(handler, SelectionEvent.getType()); 
} 

@Override 
public HandlerRegistration addKeyDownHandler(final KeyDownHandler handler) 
{ 
    return mySuggestionBox.addHandler(handler, KeyDownEvent.getType()); 
} 
} 

XML模板:

</ui:style> 
    <g:SuggestBox ui:field="mySuggestionBox"></g:SuggestBox> 
</ui:UiBinder> 
+2

您應該發佈您迄今爲止使用過的代碼。這將有助於弄清楚什麼是錯的。 – enrybo 2013-03-15 03:34:23

+1

發佈您的代碼。 – 2013-03-15 03:55:09

+1

你必須做productNameString.contains(searchString)。使用productNameString.startsWith(searchString) – Adarsha 2013-03-15 07:14:04

回答

0

我認定了什麼差錯在我的代碼。我通過調用clearOracle()來清除建議列表;然後添加新的建議。有效。感謝大家。

private void populateOracle(final String inputText) 
{ 

    String searchText = ""; 
    if(inputText != null) 
    { 
     searchText = inputText.trim(); 
    } 
    clearOracle(); 
    if((searchText == null || searchText.length() < 1)) 
    { 

     mySuggestionBox.showSuggestionList(); 
     return; 
    } 
    final List<String> oracleSuggestions = new ArrayList<String>(); 
    for(String scope : PDT_CHANNELS) 
    { 
     oracleSuggestions.add(searchText + " in " + scope); 
    } 
    populateOracle(oracleSuggestions); 

}