【问题标题】:SuggestBox GWT showing all options on Enter keySuggestBox GWT 显示 Enter 键上的所有选项
【发布时间】:2010-06-14 17:33:48
【问题描述】:

我需要创建一个 SuggestBox,它会在按下时显示所有选项 回车键。 我已经编写了以下实现,它似乎是 工作正常。 我希望有人审查我的实施并让我知道它是否 在任何特定情况下都会导致问题。 此外,要传递给这个 SuggestBox 的 SuggestOracle 应该有 默认建议集,通过调用方法 MultiWordSuggestOracle 上的 setDefaultSuggestions()。我的任何用户 SuggestBox 对这一事实应该是透明的。因此我想我会 需要包装(或扩展)MultiWordSuggestOracle 来做默认 建议设置。你能推荐什么好方法吗 这样做?

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

【问题讨论】:

    标签: gwt


    【解决方案1】:

    这对我来说看起来很不错。另一种方法是添加一个按钮来显示所有建议。该按钮的样式可以看起来像一个下拉框箭头。

    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】:

      这种方法可能会出现问题,因为当您单击按钮时会失去对建议框的关注,因此触发模糊验证等的人可能会受到这种行为的干扰。

      我推荐使用这个为现实生活中的大型应用程序创建的库,由于使用后的反馈,解决了许多实际案例 http://code.google.com/p/advanced-suggest-select-box/

      对于第一个原始问题,您可以覆盖 onkeyUp() 并为您的案例做一些特殊的事情,然后委托给 super.onKeyUp() 处理所有其他问题。

      希望能帮到你 此致, 齐德·哈姆迪

      【讨论】:

        【解决方案3】:

        @Vishal 辛格,

        无需扩展 SuggestBox。 您的代码适用于基本的 SuggestBox。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-08
          • 2019-03-14
          • 1970-01-01
          相关资源
          最近更新 更多