GWT Suggestion框覆盖默认行为

时间:2013-03-14 23:05:24

标签: gwt override suggestbox

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

例如,如果产品分类为男士,女士,儿童,夏季,冬季和用户输入“穿着”,建议清单应显示如下:

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> 

1 个答案:

答案 0 :(得分: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);

}