在suggestBox()中显示图像+文本

时间:2014-08-10 16:26:45

标签: gwt suggestbox

我试图在下面的代码中使用SuggestBox和MultiWordSuggestOracle在建议下拉列表中显示图标和文本:

public class Suggestions implements Suggestion {

    private String suggestion;

    public Suggestions(){}

    public Suggestions(String suggestion){
        this.suggestion = new String( suggestion );
    }
    @Override
    public String getDisplayString() {
        return ( suggestion + " <img src='/images/image.png'/> " );
    }
    @Override
    public String getReplacementString() {
        return suggestion;
    }}

在onModuleLoad函数中包含以下代码:

 MultiWordSuggestOracle oracle = new MultiWordSuggestOracle(){
          @Override
          public boolean isDisplayStringHTML() {
              return true;
          }};  
          oracle.add(new Suggestions("A").getDisplayString());
      SuggestBox suggestionBox = new SuggestBox(oracle);

问题:html代码打印为普通文本。你们可以建议代码有什么问题吗?

感谢名单!

2 个答案:

答案 0 :(得分:1)

因为您的Suggestion类从未使用过!

多字oracle只有显示字符串,并创建自己的Suggestion项目。

只需覆盖oracle中的方法createSuggestion:

MultiWordSuggestOracle oracle = new MultiWordSuggestOracle(){
      @Override
      public boolean isDisplayStringHTML() {
          return true;
      }

      @Override
      protected MultiWordSuggestion createSuggestion(String replacementString, String displayString) {
         return new Suggestions(replacementString);
      }
};  
oracle.add(new Suggestions("A").getDisplayString());
SuggestBox suggestionBox = new SuggestBox(oracle);

答案 1 :(得分:1)

以下是我在建议中删除图片+文字所做的工作。

public class Suggestions implements Suggestion {

    private String suggestion;

    public Suggestions(){}

    public Suggestions(String suggestion){
        this.suggestion = new String( suggestion );
    }
    @Override
    public String getDisplayString() {
        return ( suggestion + new Image('/images/image.png') );
    }
    @Override
    public String getReplacementString() {
        return suggestion;
    }}

OnModuleLoad function is: 

MultiWordSuggestOracle oracle = new MultiWordSuggestOracle(){
      @Override
      public boolean isDisplayStringHTML() {
          return true;
      }

      @Override
      protected MultiWordSuggestion createSuggestion(String replacementString, String displayString) {
         return new Suggestions(replacementString);
      }
};  
oracle.add("A");
SuggestBox suggestionBox = new SuggestBox(oracle);