1

我有一个用于在我的应用程序中搜索产品的文本框。当用户在文本框中输入内容时,它应该显示建议,以便用户可以搜索任何特定的产品类别。

例如,如果产品分类为男士、女士、儿童、夏季、冬季,并且用户输入“鞋类”,则建议列表应显示如下:

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

在 KeyUp 事件中,我清除当前建议并通过在每个类别名称中附加用户输入的文本来填充新的建议列表。只要用户没有输入任何与类别名称匹配的内容,它就可以正常工作。也就是说,如果用户输入“Win”,则建议仅显示

Wi in Winter

但我希望这些建议是

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

谁能告诉我可能出了什么问题。

谢谢。

更新 - 附上代码

我实际上创建了一个自定义小部件。下面是我的 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> 
4

1 回答 1

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);

}
于 2013-03-17T23:51:56.130 回答